gpt4 book ai didi

ios - 如何防止像 iPhone 照片应用程序那样自动旋转?

转载 作者:行者123 更新时间:2023-11-29 10:45:38 27 4
gpt4 key购买 nike

我想阻止 UICollectionViewController 在屏幕上有手指时自动旋转。手指可以移动,设备可以旋转,但只要手指仍在屏幕上,UICollectionViewController 就不应旋转。

当手指离开屏幕时,UICollectionViewController 应该立即旋转。正如 iPhone 照片应用程序所做的那样。

问题:

  1. 如何检测触摸?

    我覆盖了 UICollectionView 子类中的 touchBegan:withEvent: 等。但是当 UICollectionView 开始滚动时,它会调用 touchCanceled:withEvent: 方法。

    如果我更早开始滚动 UICollectionView,touchBegan:withEvent: 甚至不会触发。

  2. 如何暂时防止自动旋转?

    我覆盖了我的 View Controller 中的 shouldAutorotate 以防止旋转。但是当手指离开屏幕时,UICollectionView 不能立即旋转。

最佳答案

请试试这段代码:

@interface BlockAutorotateViewController ()

@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *swipeGestureRecognizer;
@property (nonatomic, assign, getter = isPressed) BOOL pressed;

@end

@implementation BlockAutorotateViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.pressed = NO;

self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(myLongPressAction:)];
self.longPressGestureRecognizer.minimumPressDuration = 0;
[self.view addGestureRecognizer:self.longPressGestureRecognizer];

self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(mySwipeAction:)];
[self.view addGestureRecognizer:self.swipeGestureRecognizer];

}

- (void)myLongPressAction:(id)sender
{
if ((self.longPressGestureRecognizer.state == UIGestureRecognizerStateEnded) || (self.longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)) {
self.pressed = YES;
}
else if (self.longPressGestureRecognizer.state == UIGestureRecognizerStateBegan) {
self.pressed = NO;
[UIViewController attemptRotationToDeviceOrientation];
}
else {
self.pressed = NO;
}
}

- (void)mySwipeAction:(id)sender
{
if ((self.swipeGestureRecognizer.state == UIGestureRecognizerStateBegan) || (self.longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)) {
self.pressed = YES;
}
else if (self.longPressGestureRecognizer.state == UIGestureRecognizerStateEnded) {
self.pressed = NO;
[UIViewController attemptRotationToDeviceOrientation];
}
else {
self.pressed = NO;
}
}

- (BOOL)shouldAutorotate
{
return (self.isPressed ? NO : YES);
}

@end

关于ios - 如何防止像 iPhone 照片应用程序那样自动旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22498503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com