Objective-C uiview点击按钮使uiview横向和全屏显示,再次点击时恢复原样
时间: 2024-01-14 09:04:28 浏览: 107
好的,你可以通过以下Objective-C代码实现该功能:
首先,在你的UIViewController中添加以下属性:
```
@property (nonatomic, assign) BOOL isFullScreen;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
```
然后,在你的UIViewController的viewDidLoad方法中添加如下代码:
```
self.isFullScreen = NO;
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[self.view addGestureRecognizer:self.tapGesture];
```
接下来,实现handleTapGesture方法:
```
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
if (self.isFullScreen) {
[UIView animateWithDuration:0.3 animations:^{
self.view.transform = CGAffineTransformIdentity;
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}];
self.isFullScreen = NO;
} else {
[UIView animateWithDuration:0.3 animations:^{
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
self.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}];
self.isFullScreen = YES;
}
}
```
这里我们同样使用UIView的transform属性来实现旋转,使用CGAffineTransformMakeRotation方法来创建旋转变换矩阵。不同的是,我们在全屏显示时将UIView的宽和高调换了一下,这样就能实现横向全屏显示了。
当用户点击UIView时,我们会先判断当前UIView是否已经全屏显示,如果已经全屏显示,则执行恢复原样的动画;否则,执行全屏显示的动画。
希望这个代码能够对你有所帮助!
阅读全文