仿微信摇一摇 ios

来源:互联网 发布:淘宝2015年销售额 编辑:程序博客网 时间:2024/06/14 20:20

iOS 中有摇动要实现的方法:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);

- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);


我们需要在ViewController的viewDidLoad方法中加入

  //可以晃动

 [[UIApplication sharedApplicationsetApplicationSupportsShakeToEdit:YES];

    //成为第一响应

 [self becomeFirstResponder];

//然后去实现那几个方法就可以了


- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event


{

    

    //检测到摇动

    NSLog(@"开始摇动");

    

    //振动效果

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    //upImageView上移

    CABasicAnimation *upAnimation = [CABasicAnimationanimationWithKeyPath:@"position.y"];

    upAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    upAnimation.fromValue = @([UIScreen mainScreen].bounds.size.height/4);

    upAnimation.toValue = @(0);

    upAnimation.duration = 1;

    upAnimation.repeatCount = 1;

    upAnimation.autoreverses = YES;

    [self.upImageView.layer addAnimation:upAnimation forKey:@"upAnimation"];

    //downImageView下移

    CABasicAnimation *downAnimation = [CABasicAnimationanimationWithKeyPath:@"position.y"];

    downAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    downAnimation.fromValue = @([UIScreen mainScreen].bounds.size.height/4*3);

    downAnimation.toValue = @([UIScreen mainScreen].bounds.size.height);

    downAnimation.duration = 1;

    downAnimation.repeatCount = 1;

    downAnimation.autoreverses = YES;

    

    

    

    [self.downImageView.layer addAnimation:downAnimation forKey:@"downAnimation"];

}




- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event


{

    

    //摇动取消

    NSLog(@"取消摇动");

    

}




- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event


{

    

    //摇动结束

     NSLog(@"摇动结束");

    [self.activity stopAnimating ];

    

    if (event.subtype == UIEventSubtypeMotionShake) {

        

        //something happens

        

    }

    

}



1 0
原创粉丝点击