一起來給各位朋友介紹一篇關於蘋果開發之iOS 搖一搖動畫效果了,希望這篇教程可以為各位帶來幫助。
微信的搖一搖動畫效果看起來很棒,這裡是類似的搖一搖動畫效果,代碼示例:
代碼如下
復制代碼
#pragma mark - 搖一搖
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //震動效果
[self shakeView:phoneImgView]; //這裡的phoneImgView是需要搖動效果的圖片控件
}
}
#pragma mark - 動畫
-(void)shakeView:(UIView*)viewToShake
{
CGFloat t = 14.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.08 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:5.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished){
if(finished){
[UIView animateWithDuration:0.08 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform =CGAffineTransformIdentity;
} completion:NULL];
}
}];
}
注:需要震動效果時,要導入AudioToolbox.framework,然後導入頭文件”AudioToolbox/AudioToolbox.h”,實現的搖一搖動畫效果看起來還不錯,可以在自己的工程裡試一試。