创建一个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对象,添加两张图片,再调用切换图片方法即可。