.h
#import <UIKit/UIKit.h>
@interface ZSViewController : UIViewController
{
float screenWidth;
float screenHeight;
}
-(UIColor*)createColor:(float)r :(float)g :(float)b;
@end
.m
#import "ZSViewController.h"
@interface ZSViewController ()
@end
@implementation ZSViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.view.backgroundColor = [UIColor colorWithRed:100.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.8];
}
return self;
}
-(UIColor*)createColor:(float)r :(float)g :(float)b
{
UIColor *color = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:0.8];
return color;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//-------------0.init
screenWidth = self.view.center.x*2;
screenHeight = self.view.center.y*2;
//-------------1.add a new view
UIView *picSwitchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
picSwitchView.backgroundColor = [self createColor:100.0 :200.0 :200.0];
picSwitchView.tag = 1000;
[self.view addSubview:picSwitchView];
//-------------2.add image view
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picture1.jpg"]];
[imageView1 setFrame:CGRectMake(0,0, screenWidth, screenHeight)];
imageView1.tag = 1001;
imageView1.backgroundColor = [self createColor:255.0 :0 :0];
[picSwitchView addSubview:imageView1];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picture2.jpg"]];
[imageView2 setFrame:CGRectMake(0,0, screenWidth, screenHeight)];
imageView2.tag = 1002;
imageView2.backgroundColor = [self createColor:0.0 :255.0 :0.0];
[picSwitchView addSubview:imageView2];
//-------------3.add btn to control fanzhuan
UIButton *btnControl = [UIButton buttonWithType:UIButtonTypeSystem];
[btnControl setFrame:CGRectMake(screenWidth/2, screenHeight-30, 40, 20)];
[btnControl setTitle:@"fanzhuan" forState:UIControlStateNormal];
[btnControl addTarget:self action:@selector(fanzhuan) forControlEvents:UIControlEventTouchUpInside];
[picSwitchView addSubview:btnControl];
}
-(void)fanzhuan
{
NSLog(@"btn clicked!");
//1.animation operator
CGContextRef theContext = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:theContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
//2.add anination to a view
UIView *thePicSwitchView = [self.view viewWithTag:1000];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:thePicSwitchView cache:YES];
NSInteger purple = [[thePicSwitchView subviews] indexOfObject:[thePicSwitchView viewWithTag:1002]];
NSInteger maron = [[thePicSwitchView subviews] indexOfObject:[thePicSwitchView viewWithTag:1001]];
NSLog(@"tag 1002 is :%d,tag 1001 is :%d",(int)purple,(int)maron);
[thePicSwitchView exchangeSubviewAtIndex:maron withSubviewAtIndex:purple];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished)];
[UIView commitAnimations];
}
-(void)animationFinished
{
NSLog(@"animation finished!");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end