图片的切换动画 CATransition

本文介绍如何使用Objective-C在iOS应用中实现两个圆形头像图片的切换动画效果,并通过手势识别来触发图片的更换。

   

UIView* myView;
    myView =[[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
    imageOne =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"psb.jpeg"]];
    imageOne.frame =CGRectMake(0, 0, 200, 200);
    imageOne.userInteractionEnabled =YES;
    imageOne.layer.cornerRadius = imageOne.frame.size.height /2;
    [imageOne.layer setMasksToBounds:YES];
    
     imageTwo =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"profile.png"]];
    imageTwo.frame =CGRectMake(0, 0, 200, 200);
    imageTwo.userInteractionEnabled =YES;
    imageTwo.layer.cornerRadius =imageTwo.frame.size.height/2;
    [imageTwo.layer setMasksToBounds:YES];
    [myView addSubview:imageTwo];
    [myView addSubview:imageOne];
    UITapGestureRecognizer* tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeImage:)];
    tap.numberOfTapsRequired =1;
   
    [imageOne addGestureRecognizer:tap];
    
    UITapGestureRecognizer* tap2 =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeImage:)];
    tap2.numberOfTapsRequired =1;
    [imageTwo addGestureRecognizer:tap2];
    
    myView.layer.cornerRadius =myView.frame.size.height/2;
    [myView.layer setMasksToBounds:YES];
    [self.view addSubview:myView];
    showOne =YES;
-(void)changeImage:(id) sender{
    NSLog(@"----------------");
    [UIView transitionFromView:(showOne ? imageOne :imageTwo) toView:(showOne ? imageTwo :imageOne) duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft + UIViewAnimationOptionCurveEaseIn completion:
    ^(BOOL finished) {
        showOne =!showOne;
    }];
    
    
}


CATransition做动画

  CATransition *animation =[CATransition animation];
    animation.delegate =self;
    animation.duration =3;
    animation.timingFunction =UIViewAnimationCurveEaseInOut;
    animation.type =@"oglFlip";
    
    
    NSUInteger z =[[myView subviews] indexOfObject:imageOne];
    NSUInteger f =[[myView subviews] indexOfObject:imageTwo];
    [myView exchangeSubviewAtIndex:z withSubviewAtIndex:f];
    [[myView layer]addAnimation:animation forKey:@"animation"];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值