iOS实现图片抖动效果
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了iOS实现图片抖动效果的具体代码,供大家参考,具体内容如下
效果图:
核心代码:
//
// ViewController.m
// 图标抖动
//
// Created by llkj on 2017/8/29.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "ViewController.h"
#define angle2Rad(angle) ((angle) / 180.0 *M_PI)
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageV.userInteractionEnabled = YES;
//添加长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageV addGestureRecognizer:longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress{
//创建动画对象
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@(angle2Rad(-5)),@(angle2Rad(5))];
anim.repeatCount = MAXFLOAT;
// anim.duration = 1;
anim.autoreverses = YES;
[self.imageV.layer addAnimation:anim forKey:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
小编再给大家补充一段iOS UIView视图抖动效果的实现代码:
- (void)shakeAnimationForView:(UIView *) view {
CALayer *viewLayer = view.layer;
CGPoint position = viewLayer.position;
CGPoint x = CGPointMake(position.x + 1, position.y);
CGPoint y = CGPointMake(position.x - 1, position.y);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[animation setFromValue:[NSValue valueWithCGPoint:x]];
[animation setToValue:[NSValue valueWithCGPoint:y]];
[animation setAutoreverses:YES];
[animation setDuration:.06];
[animation setRepeatCount:3];
[viewLayer addAnimation:animation forKey:nil];
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341