翻转UIView

//
//  ViewController.m
//  Test
//
//  Created by mac on 17/5/8.
//  Copyright © 2017年 cai. All rights reserved.
//

#import "ViewController.h"

#define Screen_Width [UIScreen mainScreen].bounds.size.width

#define Screen_Height [UIScreen mainScreen].bounds.size.height

@interface ViewController ()
{
    NSUInteger index;
}

@property (nonatomic, strong) UIView *baseView;

@property (nonatomic, strong) UIImageView *imgView1;

@property (nonatomic, strong) UIImageView *imgView2;

@end

@implementation ViewController

#pragma mark -懒加载
- (UIView *)baseView
{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(15, 100, Screen_Width - 30, Screen_Height - 150)];
        _baseView.backgroundColor = [UIColor cyanColor];
    }
    return _baseView;
}

- (UIImageView *)imgView1
{
    if (!_imgView1) {
        _imgView1 = [[UIImageView alloc] initWithFrame:_baseView.bounds];
        _imgView1.backgroundColor = [UIColor purpleColor];
        _imgView1.image = [UIImage imageNamed:@"car1.jpg"];
        _imgView1.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imgView1;
}

- (UIImageView *)imgView2
{
    if (!_imgView2) {
        _imgView2 = [[UIImageView alloc] initWithFrame:_baseView.bounds];
        _imgView2.backgroundColor = [UIColor purpleColor];
        _imgView2.image = [UIImage imageNamed:@"car2.jpg"];
        _imgView2.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imgView2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    index = 0;
    
    [self createUI];
}

#pragma mark -createUI
- (void)createUI
{
    [self.view addSubview:self.baseView];
    
    [self.baseView addSubview:self.imgView1];
    [self.baseView addSubview:self.imgView2];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    index ++;
    
    if (index % 2 == 0) {
        //设置动画的效果
        [UIView transitionWithView:self.baseView
                          duration:2
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:^{
                            //交换两个视图的位置
                            [self.baseView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
                        }
                        completion:^(BOOL finished) {
                            
                        }];
    }else {
        //设置动画的效果
        [UIView transitionWithView:self.baseView
                          duration:2
                           options:UIViewAnimationOptionTransitionFlipFromRight
                        animations:^{
                            //交换两个视图的位置
                            [self.baseView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
                        }
                        completion:^(BOOL finished) {
                            
                        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
复制代码

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值