UIView视图移动,抖动效果,定时器,动画block

   for (int i = 0; i<2; i++) {

        UIImageView * img = [[UIImageView alloc] initWithFrame:CGRectMake(270*i, 20+410*i, 50, 50)];

        if (i == 0) {

            img.image = [UIImage imageNamed:@"1.jpg"];

            img.tag = 101;

        }else{

            img.image = [UIImage imageNamed:@"chash1.png"];

            img.tag = 102;

        }

        img.userInteractionEnabled = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTap:)];

        [img addGestureRecognizer:tap];

        [tap release];

        [self.window addSubview:img];

    }

    

    

    return YES;

}


- (void)myTap:(UITapGestureRecognizer *)tap

{

    

    //设置动画持续时间

    

    if (tap.view.tag ==101) {

     

        // 动画执行完成后再执行某个操作 办不到

        //        [UIView beginAnimations:nil context:nil];

        //        [UIView setAnimationDuration:1.0];

        //        tap.view.frame = CGRectMake(320, 480, 5, 5);

        //        [tap.view removeFromSuperview];

        //        [UIView commitAnimations];

        // 在动画执行完成后 再执行某个操作

        // 参数1:动画持续时间 参数2:动画需要执行的效果(frame,alpha,hidden,transfrom... 参数3:动画执行完成后的操作

        [UIView animateWithDuration:1.0 animations:^{

            tap.view.frame = CGRectMake(320, 480, 5, 5);

        } completion:^(BOOL finished) {

            // 代码会在1秒后才执行

            [tap.view removeFromSuperview];

            UIImageView * imgView = (UIImageView *)[self.window viewWithTag:101];

            imgView.image = [UIImage imageNamed:@"2.png"];

        }];

        

    }

    else{

        //旋转

        UIImageView * imgView = (UIImageView *)[self.window viewWithTag:102];

        //x/30 = M_PI/180

        imgView.transform = CGAffineTransformMakeRotation(30*M_PI/180);

        /*

         启动定时器

         参数1:时间间隔

         参数2:定时器需要添加的目标对象

         参数3:需要执行的方法

         参数4:传参用

         参数5:是否需要重复执行

         每隔 参数1的时间间隔 会调用参数2对象的 @selector(参数3)的方法

         */

        if ([time isValid]) {

            //销毁定时器

            [time invalidate];

            time = nil;

        }

        time =[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(shark) userInfo:nil repeats:YES];

        imgView.image = [UIImage imageNamed:@"chash1.png"];

    }


}

int count = 0;

-(void)shark

{

    UIImageView * imgView = (UIImageView *)[self.window viewWithTag:102];

    //x/30 = M_PI/180

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.1];

    if (count %2) {

        /*

         实现UIView的旋转 缩放 平移 需要访问UIViewtransform属性

         */

        imgView.transform = CGAffineTransformMakeRotation(10*M_PI/180);

    }else{

        imgView.transform = CGAffineTransformMakeRotation(-10*M_PI/180);

    }

    [UIView commitAnimations];

    count++;

    if (count>6) {

        if ([time isValid]) {

            [time invalidate];

            time = nil;

            //图片复原

            imgView.transform = CGAffineTransformIdentity;

            imgView.image = [UIImage imageNamed:@"chash1.png"];

            count = 0;

            

        }

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值