您的位置:首页 > 移动开发 > 微信开发

iOS 仿微信摇一摇

2016-02-24 14:47 477 查看
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
sharedApplication] setApplicationSupportsShakeToEdit:YES];

//成为第一响应

[self
becomeFirstResponder];

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

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

{

//检测到摇动

NSLog(@"开始摇动");

//振动效果

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

//让upImageView上移

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

upAnimation.timingFunction = [CAMediaTimingFunction
functionWithName: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 = [CABasicAnimation
animationWithKeyPath:@"position.y"];

downAnimation.timingFunction = [CAMediaTimingFunction
functionWithName: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

}

}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: