欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS实现图片抖动效果

时间:2021-05-28 08:03:06|栏目:iOS代码|点击:

本文实例为大家分享了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视图抖动效果的实现代码:

/**
 * 抖动效果
 *
 * @param view 要抖动的view
 */
- (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];
}

上一篇:iOS 对plist文件进行读写,增删改查的实例

栏    目:iOS代码

下一篇:iOS实现Pad上菜单弹出界面

本文标题:iOS实现图片抖动效果

本文地址:http://www.codeinn.net/misctech/130783.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有