美文网首页
让图片转起来

让图片转起来

作者: shushuzhen | 来源:发表于2017-03-28 17:38 被阅读70次

先上一个gif图,看下效果:



在项目中很少用到动画,这个动画可以用于更新或者下载或者操作等待时的动画。
下面直接上代码:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 创建一个ImageView
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageView.center = self.view.center;
    imageView.image = [UIImage imageNamed:@"airCon_on"];
    [self.view addSubview:imageView];
    self.imageV = imageView;
    
    // 创建一个点击按钮
    UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [clickBtn setFrame:CGRectMake(0, 0, 100, 50)];
    clickBtn.center = CGPointMake(imageView.center.x, 100);
    [clickBtn setTitle:@"点这里呀" forState:UIControlStateNormal];
    clickBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [clickBtn addTarget:self action:@selector(clickToRotate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clickBtn];

}

- (void)clickToRotate:(UIButton *)btn{

    if (btn.selected) {
        // 启动旋转动画
       [self rotate360DegreeWithView:self.imageV];
    }else{
        // 移除旋转动画
        [self removreAnimation];
    }
    btn.selected = !btn.selected;
}
// 旋转动画
- (void)rotate360DegreeWithView:(UIView *)view{


    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    
    // 围绕Z轴旋转,垂直于屏幕
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0)];
    // 旋转持续时间
    animation.duration = 0.5;
    // 旋转效果累计,先转180度,接着再旋转180度,从而实现360度旋转
    animation.cumulative = YES;
    // 旋转次数累计
    animation.repeatCount = 1000;
    view.tag = 1000;
    // 动画要加到layer上
    [view.layer addAnimation:animation forKey:nil];
}
// 移除动画
- (void)removreAnimation{
  
    UIView *view = [self.view viewWithTag:1000];
    [view.layer removeAllAnimations];
}
@end

动画要加到view的layer上面

相关文章

  • 让图片转起来

    先上一个gif图,看下效果: 在项目中很少用到动画,这个动画可以用于更新或者下载或者操作等待时的动画。下面直接上代...

  • 爱的魔力转圈圈——CSS图片旋转

    今天和大家分享的案例是如何利用css3让图片旋转起来。 让图片动起来,我们利用“@keyframes”创建动画,“...

  • CALayer动画的开始.暂停.继续.移除

    就最近的项目中遇到的旋转动画需求 1.提供图片 让图片旋转起来2.让动画暂停以做到更好的用户体验3.暂停后必须继续...

  • iOS 图片360° 平缓旋转

    听说项目开发中难免会遇到,让图片旋转起来的(看会旋转的女孩,??);于是我就写了一个UIImageView 分类,...

  • 让脾胃转起来

    脾胃不好的人会口臭、便秘、肥胖、手脚冰冷、贫血、月经不调等各种症状,只有脾胃正常转起来,人才会健康。

  • 让煤球转起来

    “咚——咚——齐呛齐!” 高跷的锣鼓声在这小山村热闹地响了起来,年味渐渐浓了。 小山沟南北走向,沟两边隔十里左右,...

  • 《让生活转起来》

    1.我怎么如此幸运,开始感知什么是我的第一唯一? 2.我怎么如此幸运,开始不为责任而动 而为心动而动? 3.我怎么...

  • 让中医转起来

    各大医院应该利用中医 ,网上开免挂号费,为病人看病,利用快递让大家能病后可就医。 老公家侄儿带的酸...

  • 让煤球转起来(二)

    失去了绿色渲染的山头灰秃秃的,山村里,除了时不时从村口开出的绿色公交车,显得静谧异常,偶有零星的鞭炮声告诉人们,年...

  • 让飞轮转起来

    你是否和我一样,每天有那么多工作要处理,孩子等着陪伴,家人需要照顾,朋友也要联系吧,所以时常纠结于自我学习成长的时...

网友评论

      本文标题:让图片转起来

      本文链接:https://www.haomeiwen.com/subject/qmajottx.html