IOS-试图旋转效果的实现

.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值