Siri 效果实现外加UIDynamic有趣动画


@property (nonatomic, strong)UIDynamicAnimator   *animator;
@property (nonatomic, strong)UIVisualEffectView  *effectView;

// 展示Siri 效果
[self showEffectView]; 

-(UIDynamicAnimator *)animator
 {
     if (_animator==nil) {
         //创建物理仿真器(ReferenceView:参照视图,设置仿真范围)
         self.animator=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
     }
     return _animator;
 }

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self testGravity];
}

-(void)testGravity
{
    //重力行为
    UILabel *label = [[UILabel alloc] init];
    label.text = @"What can I do for you?";
    label.font = [UIFont systemFontOfSize:28];
    [label sizeToFit];
    label.center = self.view.center;
    label.transform = CGAffineTransformMakeRotation(M_PI_4 * random());
    self.redView = label;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor colorWithRed:(arc4random()%100 + 100)/255.f green:(arc4random()%100 + 100)/255.f blue:(arc4random()%100 + 100)/255.f alpha:1.00f];

    [self.effectView.contentView addSubview:label];
    
    UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
    [gravity addItem:self.redView];
    [self.animator addBehavior:gravity];
    
    UICollisionBehavior *collision = [[UICollisionBehavior alloc]init];
    collision.translatesReferenceBoundsIntoBoundary = YES;
    [collision addItem:self.redView];
    [self.animator addBehavior:collision];
}

- (void)showEffectView {
  
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:nil];
    effectView.frame = self.view.bounds;
    [self.view addSubview:effectView];
    self.effectView = effectView;
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"What can I do for you?";
    label.font = [UIFont systemFontOfSize:28];
    [label sizeToFit];
    [effectView.contentView addSubview:label];
    label.center = self.view.center;
    
    [UIView animateWithDuration:1.5 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        effectView.effect = blurEffect;
        label.center = CGPointMake(self.view.center.x, self.view.center.y - 100);
        
    } completion:^(BOOL finished) {
        
    }];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureClicked:)];
    [effectView addGestureRecognizer:tapGesture];
}

- (void)tapGestureClicked:(UITapGestureRecognizer *)tap {
    
    UIVisualEffectView *effectView = (UIVisualEffectView *)tap.view;
    
    [UIView animateWithDuration:1.5 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        //effectView.effect = nil;
        [effectView viewWithTag:110].center = CGPointMake(self.view.center.x, 0);
    } completion:^(BOOL finished) {
        
    }];
}

你可能感兴趣的:(Siri 效果实现外加UIDynamic有趣动画)