Objective-C uiview点击按钮使uiview旋转90度全屏显示,再次点击时恢复原样
时间: 2023-10-06 12:05:17 浏览: 203
UIView-UpdateAutoLayoutConstraints:一种创建和更新 AutoLayout Constraints 的简单方法(主要是更新 UIView 的 Width 和 Height)
好的,我理解了你的要求。你可以通过以下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的frame属性来使其全屏显示。
当用户点击UIView时,我们会先判断当前UIView是否已经全屏显示,如果已经全屏显示,则执行恢复原样的动画;否则,执行全屏显示的动画。
希望这个代码能够对你有所帮助!
阅读全文