先上效果图
新建项目打开storyboard,ViewController里拖入这些控件
上面的stackView设置
下面的stackView设置
为上面的stackView 添加约束
为下面的stackView 添加约束
下面添加代码
//
// ViewController.m
// 20171102
//
// Created by ZSY on 17/11/2.
// Copyright © 2017年 ZSY. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIStackView *stackView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;
@property (nonatomic,assign) BOOL isLarge;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
for (NSInteger i = 0; i < 3 ; i ++) {
UIImageView *imageView = [self valueForKey:[NSString stringWithFormat:@"imageView%zd",i + 1]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView:)];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tap];
}
}
- (void)clickImageView:(UITapGestureRecognizer *)tap {
long long tag = tap.view.tag;
self.isLarge = !self.isLarge;
switch (tag) {
case 1000:
if (!self.isLarge) {
[UIView animateWithDuration:0.5 animations:^{
self.imageView2.hidden = NO;
self.imageView3.hidden = NO;
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
self.imageView2.hidden = YES;
self.imageView3.hidden = YES;
}];
}
break;
case 1001:
if (!self.isLarge) {
[UIView animateWithDuration:0.5 animations:^{
self.imageView1.hidden = NO;
self.imageView3.hidden = NO;
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
self.imageView1.hidden = YES;
self.imageView3.hidden = YES;
}];
}
break;
case 1002:
if (!self.isLarge) {
[UIView animateWithDuration:0.5 animations:^{
self.imageView2.hidden = NO;
self.imageView1.hidden = NO;
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
self.imageView2.hidden = YES;
self.imageView1.hidden = YES;
}];
}
break;
default:
break;
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}
- (void)deviceOrientationDidChange {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
NSLog(@"deviceOrientationDidChange:%ld",(long)orientation);
if (orientation == UIDeviceOrientationPortrait) {
self.stackView.axis = UILayoutConstraintAxisVertical;
} else if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
self.stackView.axis = UILayoutConstraintAxisHorizontal;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end