[英]iOS - Make buttons rotate in still views like Camera app
我正在尝试制作类似iPhone Camera应用程序顶部和底部栏的视图。 我无法让顶视图和底视图保持肖像。
当我使用- (BOOL)shouldAutorotate
,旋转通知会停止。 我曾尝试使用setNeedsUpdateConstraints
更新约束,但仍能获得动画效果。 我希望视图被锁定并且仅UIButton可以旋转。
您如何仅使视图中的按钮旋转并保持锁定?
我有一个UIViewController,仅在旋转设备时才旋转其中的一些子视图。 (这在iOS7下工作正常,但在iOS8下工作。)您需要使用CGAffineTransform来“手动旋转”视图。
这是一些代码:
@interface VVViewController ()
@property (weak, nonatomic) IBOutlet UIView *pinnedControls;
@property (nonatomic, strong) NSMutableArray *pinnedViews;
@end
@implementation VVViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.pinnedViews = [NSMutableArray array];
[self.pinnedViews addObject:self.pinnedControls];
}
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[UIViewController rotatePinnedViews:self.pinnedViews forOrientation:self.interfaceOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
[UIViewController rotatePinnedViews:self.pinnedViews forOrientation:toInterfaceOrientation];
}
}
@end
我们在UIViewController上创建了一个类别来处理此行为。 以下是相关代码:
@implementation UIViewController (VVSupport)
+ (void)rotatePinnedViews:(NSArray *)views forOrientation:(UIInterfaceOrientation)orientation {
const CGAffineTransform t1 = [UIViewController pinnedViewTansformForOrientation:orientation counter:YES];
const CGAffineTransform t2 = [UIViewController pinnedViewTansformForOrientation:orientation counter:NO];
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
// Rotate the view controller
view.transform = t1;
[view.subviews enumerateObjectsUsingBlock:^(UIView *counterView, NSUInteger idx, BOOL *stop) {
// Counter-rotate the controlsUIin the view controller
counterView.transform = t2;
}];
}];
}
+ (CGAffineTransform)pinnedViewTansformForOrientation:(UIInterfaceOrientation)orientation counter:(BOOL)counter {
CGAffineTransform t;
switch ( orientation ) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
t = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationLandscapeLeft:
t = CGAffineTransformMakeRotation(counter ? M_PI_2 : -M_PI_2);
break;
case UIInterfaceOrientationLandscapeRight:
t = CGAffineTransformMakeRotation(counter ? -M_PI_2 : M_PI_2);
break;
}
return t;
}
@end
现在,这在iOS8下无法完美运行,请参见UIView在iOS8下使用CGAffineTransform旋转时无法调整大小的问题。
在Swift 5中 ,并使用transform
属性手动旋转。
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange),
name: UIDevice.orientationDidChangeNotification, object: nil)
}
@objc private func deviceOrientationDidChange() {
let viewsToRotate = [button1, button2, labelXX, viewXX]
let rotation = UIDevice.current.orientation.rotationTransform
viewsToRotate.forEach {
$0.transform = rotation
}
}
extension UIDeviceOrientation {
var rotationTransform: CGAffineTransform {
switch self {
case .landscapeLeft: return CGAffineTransform(rotationAngle: .pi/2)
case .landscapeRight: return CGAffineTransform(rotationAngle: -.pi/2)
case .portraitUpsideDown: return CGAffineTransform(rotationAngle: .pi)
default: return .identity
}
}
}
我在这里的博客文章中有详细信息: https : //samwize.com/2019/08/06/how-to-rotate-selected-views-like-camera-app/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.