本文仿抖音实现摇一摇功能,可触发震动并通过点击跳转到下一界面 。

本文采用的宏定义

#define kwidth [UIScreen mainScreen].bounds.size.width

#define kheight [UIScreen mainScreen].bounds.size.height

#define ktopHeight [UIApplication sharedApplication].statusBarFrame.size.height

1、摇一摇调用

(1)摇一摇检测

使用以下代理方法,晃动手机即可调用,但是无法实现震动

//检测到开始摇动

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

//摇一摇被取消或中断

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

//摇动结束

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

(2)添加震动效果

导入:#import

在需要出发震动的地方写上代码: AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//默认震动效果

如果想要其他震动效果,可参考: // 普通短震,3D Touch 中 Pop 震动反馈 AudioServicesPlaySystemSound(1520);

// 普通短震,3D Touch 中 Peek 震动反馈 AudioServicesPlaySystemSound(1519);

// 连续三次短震 AudioServicesPlaySystemSound(1521);

本文中使用的是AudioServicesPlaySystemSound(1521);

2、动画

添加摇一摇视图,使用的是CABasicAnimation

- (void)addShakeView {

if (_shakeView.isShow) {

return;

} else {

_shakeView = [[ZWShakeView alloc] initWithFrame:CGRectMake(15, ktopHeight, kwidth - 30, 80)];

_shakeView.backgroundColor = [UIColor whiteColor];

_shakeView.layer.cornerRadius = 10;

self.shakeView.isShow = YES;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];

animation.duration = 0.5;

animation.repeatCount = 1;

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(kwidth / 2, -40)];

animation.toValue = [NSValue valueWithCGPoint:CGPointMake(kwidth / 2, ktopHeight + 40)];

[_shakeView.layer addAnimation:animation forKey:@"moveIn"];

[self.view addSubview:_shakeView];

}

}

等到摇动结束调用

//摇动结束

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

// AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//默认震动效果

// 普通短震,3D Touch 中 Pop 震动反馈

// AudioServicesPlaySystemSound(1520);

// 普通短震,3D Touch 中 Peek 震动反馈

// AudioServicesPlaySystemSound(1519);

AudioServicesPlaySystemSound(1521);

NSLog(@"摇动结束");

/*采用延时执行而不使用以下的方式,因为加上动画后会导致按钮无法点击,所以在一定时间后再加上动画

[UIView animateWithDuration:0.5 delay:3 options:UIViewAnimationOptionCurveEaseOut animations:^{

self.shakeView.frame = CGRectMake(15, -80, kwidth - 30, 80);

} completion:^(BOOL finished) {

[self.shakeView removeFromSuperview];

}];

*/

[self performSelector:@selector(moveShakeView) withObject:nil afterDelay:3];

}

- (void)moveShakeView {

[UIView animateWithDuration:0.5 animations:^{

self.shakeView.frame = CGRectMake(15, -80, kwidth - 30, 80);

} completion:^(BOOL finished) {

[self.shakeView removeFromSuperview];

self.shakeView.isShow = NO;

}];

}

3、navbar的隐藏

在首页需要隐藏navbar,否则会遮盖住摇一摇的视图导致无法点击,为了防止进入新页面返回后navbar重新出现,在本界面出现后隐藏navbar。如有需要,在新界面开启navbar。

-(void)viewWillAppear:(BOOL)animated {

self.navigationController.navigationBar.hidden = YES;//防止navigationBar遮住摇一摇的视图

// [_shakeView removeFromSuperview];

}

4、在View中实现控制器的跳转

/// 获取Nav

- (UINavigationController *)getNavigationController {

UIWindow *window = [UIApplication sharedApplication].keyWindow;

if ([window.rootViewController isKindOfClass:[UINavigationController class]]) {

return (UINavigationController *)window.rootViewController;

} else if ([window.rootViewController isKindOfClass:[UITabBarController class]]) {

UIViewController *selectedVC = [((UITabBarController *)window.rootViewController)selectedViewController];

if ([selectedVC isKindOfClass:[UINavigationController class]]) {

return (UINavigationController *)selectedVC;

}

}

return nil;

}

详细代码ZWAnimationShake,此功能为自己研究,如有更好的方法可一起讨论。

文章来源

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。