.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;
}