IOS几种类型的动画

1.点击按钮,弹出View,再次点击,View回去
      创建一个Button,并给button设置一个Bool值,在button的点击事件里写:
           - (void)viewBtnAction:(UIButton *)btn{
   
   
if (self.viewbtn.selected) {
        [
UIView animateWithDuration:0.5 animations:^{
           
self.myView.frame = CGRectMake(0, 677, 375, 50);
           
NSLog(@"===== %@", NSStringFromCGRect(self.myView.frame));
        }];
    }
else{
   
   
//view动画
   
//参数1:动画时长
    [
UIView animateWithDuration:0.5 animations:^{
        self.myView.frame = CGRectMake(0, 577, 375, 50);
        NSLog(@"===== %@",NSStringFromCGRect(self.myView.frame));
    }];
}

2.点击按钮.弹出View,过几秒,View自动回去
          创建一个Button,在button的点击事件里写:
- (void)viewBtnAction:(UIButton *)btn{
     [UIView animateWithDuration:0.5 animations:^{
            self.myView.frame = CGRectMake(0, 677, 375, 50);
           
NSLog(@"===== %@", NSStringFromCGRect(self.myView.frame));
        }];
     //delay 延迟几秒执行
    [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
       
self.myView.frame = CGRectMake(0, 667, 375, 50);
    }
completion:^(BOOL finished) {
       
    }];
}

3.键盘上的自定义动画框
      //监听键盘弹起
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

- (void)keyboardWillShow:(NSNotification *)notif{
   
//得到键盘frame,通过键盘frame改变自定义工具栏高度
   
CGRect frame = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
   
NSLog(@"frame == %@", NSStringFromCGRect(frame));
   
//view动画
   
//参数1:动画时长
    [
UIView animateWithDuration:0.5 animations:^{
       
self.myView.frame = CGRectMake(0, 627 - frame.size.height, 375, 50);
       
NSLog(@"===== %@", NSStringFromCGRect(self.myView.frame));
    }];
}

4. Transform动画
         创建一个View,和一个按钮,在按钮的点击事件里写
- (void)TransformBtnAction:(UIButton *)btn{
   
   
//transformView的一个属性,是用来改变View的形态的,通过设置transform属性值可以实现View的形态变化,比如旋转,缩放等
   
//rotate:旋转的意思  M_PI180
//    self.myView.transform = CGAffineTransformRotate(self.myView.transform, M_PI_4);
   
//每次缩放原来的0.9
//    self.myView.transform = CGAffineTransformScale(self.myView.transform, 0.9, 0.9);
   
//移动  正数代表往右下方移动,负数代表往左上方移动
    self.myView.transform = CGAffineTransformTranslate(self.myView.transform, 5, 5);
}

5.layer动画
     创建一个Button控制,点击旋转,再次点击停止,再点击又继续;在View上添加一张图片,并给图片设置轻拍,可以点击图片控制旋不旋转,并设置Bool值;
     在Button的点击事件里写:
      - (void)layerBtnAction:(UIButton *)btn{
   
//layer,每个View视图都有一个layer,使用来设置View上的内容,比如背景颜色,frame,文字等内容,View只用来负责显示layer
   
//我们可以通过改变layer层的内容
   
   
//改变view  z轴值
   
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
   
//旋转起始值
    animation.
fromValue = [NSNumber numberWithInt:0];
   
//最终旋转的角度
    animation.
toValue = [NSNumber numberWithInt:M_PI * 2];
   
//旋转时间
    animation.
duration = 10;
   
//重复次数,NSIntegerMax为无限旋转
    animation.
repeatCount = NSIntegerMax;
   
//旋转结束后是否要逆向返回原位置
    animation.
autoreverses = NO;
   
//是否按照结束位置继续旋转
    animation.
cumulative = YES;
    [
self.imgV.layer addAnimation:animation forKey:@"basicAnimation"];
   
}
     在轻拍事件里写:
- (void)tapImg:(UITapGestureRecognizer *)tap{
   
    if (self.isSelected) {
        //获得上次停止的时间的偏移量
       
CFTimeInterval stopTime = self.imgV.layer.timeOffset;
       
self.imgV.layer.beginTime = 0;
       
//设置速度
       
self.imgV.layer.speed = 1.0;
       
//设置偏移量为0
       
self.imgV.layer.timeOffset = 0;
       
//设置开始时间
       
self.imgV.layer.beginTime = [self.imgV.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
    }
else{
       
       
//每一个viewlayer层系统设置记录了一个时间的属性,通过改变view动画时间来控制动画效果
       
//获得当前旋转的时间点
       
CFTimeInterval stopTime = [self.imgV.layer convertTime:CACurrentMediaTime() fromLayer:nil];
       
//设置播放速度变成0,即停止
       
self.imgV.layer.speed = 0;
       
self.imgV.layer.timeOffset = stopTime;
    }
   
self.isSelected = !self.isSelected;
   
}

6.长按晃动
     在一个View上添加长按手势
      UILongPressGestureRecognizer *longView = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longView:)];
    [self.myView addGestureRecognizer:longView];
- (void)longView:(UILongPressGestureRecognizer *)longTap{
   
   
//旋转
   
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
   
float top = M_PI / 18;
   
float bom = -M_PI / 18;
    keyAnimation.
values = @[@(top),@(0),@(bom),@(0),@(top)];
    keyAnimation.
repeatCount = NSIntegerMax;
    keyAnimation.
duration = 0.3;
    [
self.myView.layer addAnimation:keyAnimation forKey:@"key"];
   
   
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
//停止动画
        [
self.myView.layer removeAnimationForKey:@"key"];
       
    });
}

7.关键帧动画:
    创建一个按钮,在按钮上放一张没颜色图片,点击按钮,变成另一张带颜色图片,在按钮的点击事件里写:
- (void)zanBtnAction:(UIButton *)btn{
   
   
//关键帧动画
   
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
   
//设置关键帧的值,values是数组类型
    keyAnimation.
values = @[@(0.1),@(1.0),@(1.5)];
//    NSNumber *num = [NSNumber numberWithFloat:0.1];
//    keyAnimation.values = [NSArray arrayWithObjects:(nonnull id), ..., nil];
    keyAnimation.
duration = 0.3;
    [
self.zanBtn.layer addAnimation:keyAnimation forKey:@"key"];
   
   
if (self.zanBtn.isSelected == NO) {
        [
self.zanBtn setBackgroundImage:[UIImage imageNamed:@"zan4.jpg"] forState:UIControlStateNormal];
    }
else{
        [
self.zanBtn setBackgroundImage:[UIImage imageNamed:@"zan2.jpg"] forState:UIControlStateNormal];
    }
   
self.zanBtn.selected = !self.zanBtn.selected;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值