欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

iOS实现图片抖动效果

程序员文章站 2023-02-22 15:34:34
本文实例为大家分享了ios实现图片抖动效果的具体代码,供大家参考,具体内容如下效果图:核心代码://// viewcontroller.m// 图标抖动//// created by llkj on...

本文实例为大家分享了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 图片抖动