水波纹实现两张图片的切换

  创建一个View类,实现这个View上的两张图片切换的时候出现水波纹动画。

  首先,引入QuartzCore.framework系统库

   .h文件:

#import <UIKit/UIKit.h>

@interface AniImageView : UIView

-(void)createImageView:(UIImage *)image1 secondImageView:(UIImage *)image2;
//切换两张图片
-(void)transformImageviews;
@end

.m文件:

#import "AniImageView.h"
#import <QuartzCore/QuartzCore.h>
@interface AniImageView()
{
    CATransition *animation;
}
@end
@implementation AniImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        animation=[CATransition animation];
        animation.delegate=self;
        animation.duration=1.8;
        animation.type=@"rippleEffect";
        animation.subtype=kCATransitionFromRight;
    }
    return self;
}

-(void)createImageView:(UIImage *)image1 secondImageView:(UIImage *)image2
{
    CGRect Frame=self.frame;
    UIImageView *imageView1=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Frame.size.width, Frame.size.height)];
    imageView1.image=image1;
    imageView1.tag=100;
    [self addSubview:imageView1];
    
    UIImageView *imageView2=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Frame.size.width, Frame.size.height)];
    imageView2.image=image2;
    imageView2.tag=200;
    [self addSubview:imageView2];
    
}
-(void)transformImageviews
{
    NSInteger front=[[self subviews]indexOfObject:[self viewWithTag:100]];
    NSInteger back=[[self subviews] indexOfObject:[self viewWithTag:200]];
    [self exchangeSubviewAtIndex:front withSubviewAtIndex:back];
    [[self layer] addAnimation:animation forKey:@"animation"];
}

使用时,创建一个AniImageView对象,添加两张图片,再调用切换图片方法即可。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值