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 allocinitWithFrame:CGRectMake(1010300240)];

bgView.backgroundColor = [UIColor lightGrayColor];

bgView.tag = 150;

 

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

[bgView addSubview:imgView];

imgView.tag = 151;

imgView.center = CGPointMake(150120);

 

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

[bgView addSubview:imgView2];

imgView2.tag = 152;

imgView2.center = CGPointMake(150120);

 

UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

startBtn.frame = CGRectMake(1202608025);

[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 subviewsindexOfObject:[bgView viewWithTag:151]];

NSInteger back = [[bgView subviewsindexOfObject:[bgView viewWithTag:152]];

if (isBlue) {

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

}else {

[bgView exchangeSubviewAtIndex:back withSubviewAtIndex:front];

}


 

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

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

 

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

isBlue = !isBlue;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值