读书人

CATransition完成几种卡通效果

发布时间: 2013-03-01 18:33:02 作者: rapoo

CATransition完成几种动画效果

.h:

@interface XXXViewController : UITViewController {

int direction;

BOOL isBlue;

}

.m:


头部导入#import <QuartzCore/QuartzCore.h>

- (void)viewDidLoad {

[super viewDidLoad];


direction = 0;

isBlue = YES;

UISegmentedControl *sc = [[UISegmentedControl alloc] initWithItems:[@"Ripple Curl Uncurl Suck"componentsSeparatedByString:@" "]];

sc.segmentedControlStyle = UISegmentedControlStyleBar;

sc. selectedSegmentIndex = 0;

self.navigationItem.titleView = [sc autorelease];

UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 240)];

bgView.backgroundColor = [UIColor lightGrayColor];

bgView.tag = 150;

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:@"BFlyCircle.png"]];

[bgView addSubview:imgView];

imgView.tag = 151;

imgView.center = CGPointMake(150, 120);

UIImageView *imgView2 = [[UIImageView alloc] initWithImage:[UIImageimageNamed:@"BFlyCircleMaroon.png"]];

[bgView addSubview:imgView2];

imgView2.tag = 152;

imgView2.center = CGPointMake(150, 120);

UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[startBtn setTitle:@"开始" forState:UIControlStateNormal];

startBtn.frame = CGRectMake(120, 260, 80, 25);

[startBtn addTarget:self action:@selector(startAction:)forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:bgView];

[self.view addSubview:startBtn];

}

- (void) startAction:(id) sender{

// 定义动画

CATransition *animation = [CATransition animation];

animation.delegate = self;

animation.duration =1.0f;

animation.timingFunction = UIViewAnimationCurveEaseInOut;

//可根据需要,设置type和subtype属性产生不同的动画效果

//animation.type = kCATransitionPush;

//animation.type = kCATransitionMoveIn;

//animation.type = kCATransitionReveal;

//animation.type = kCATransitionFade;

//animation.subtype = kCATransitionPush;

//animation.subtype = kCATransitionMoveIn;

//animation.subtype = kCATransitionReveal;

//animation.subtype = kCATransitionFade;


switch ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) {

case 0:

animation.type = @"rippleEffect";

//animation.type = @"cube"; //立方体翻转

//animation.type = @"oglFlip"; //层翻转

//animation.type = @"cameraIrisHollowClose";

//animation.type = @"cameraIrisHollowOpen";

break;

case 1:

animation.type = @"pageCurl";

break;

case 2:

animation.type = @"pageUnCurl";

break;

case 3:

animation.type = @"suckEffect";

break;

default:

break;

}


switch (direction) { //方向

case 0:

animation.subtype = kCATransitionFromRight;

break;

case 1:

animation.subtype = kCATransitionFromTop;

break;

case 2:

animation.subtype = kCATransitionFromLeft;

break;

case 3:

animation.subtype = kCATransitionFromBottom;

break;

default:

break;

}

//执行动画

UIView *bgView = [self.view viewWithTag:150];

NSInteger front = [[bgView subviews] indexOfObject:[bgView viewWithTag:151]];

NSInteger back = [[bgView subviews] indexOfObject:[bgView viewWithTag:152]];

if (isBlue) {

[bgView exchangeSubviewAtIndex:front withSubviewAtIndex:back]; //图形变换

}else {

[bgView exchangeSubviewAtIndex:back withSubviewAtIndex:front];

}


[[bgView layer] addAnimation:animation forKey:@"animation"]; //bgView层执行动画

//[[self.view layer] addAnimation:animation forKey:@"animation"]; //self.view层执行动画

if(++direction > 3) direction -= 4;

isBlue = !isBlue;

}


读书人网 >移动开发

热点推荐