[英]How to rotate UIAlertController in Swift
我有一個工作的UIAlertController
,但我想將alert.view
向左旋轉 90 度。
我該怎么做? 我的代碼如下:
let alert = UIAlertController(title: "", message: "Message Sample", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Okay", style: .Default){(action)->() in })
presentViewController(alert, animated: true) {}
我試圖添加:
alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
但它不起作用。
謝謝 !
使用此代碼:
let alert = UIAlertController(title: "", message: "Message Sample", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Okay", style: .Default){(action)->() in })
self.presentViewController(alert, animated: true, completion: {() -> Void in
alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
})
Swift3
let alert = UIAlertController(title: "", message: "Message Sample", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default){(action)->() in })
self.present(alert, animated: true, completion: {() -> Void in
alert.view.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2) )
})
你可以實現這個目標:
更新的答案
self.presentViewController(alert, animated: true, completion: {() -> Void in
// alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
UIView.animateWithDuration(1.0, delay: 0, options: .CurveLinear, animations: { () -> Void in
alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
}) { (finished) -> Void in
// do something if you need
}
})
我認為這可能會更常出現在FaceID上。 我必須解決這個問題,因為我有一個處於橫向模式的應用程序,但是當用戶在iPhone上啟動應用程序或者當他們必須使用FaceID對應用程序進行身份驗證時,它們通常是縱向的。 所以我想根據用戶拿着手機的方式顯示提醒。 我的解決方案是擴展UIAlertController類,如下所示;
extension UIAlertController {
open override func viewWillAppear(_ animated: Bool) {
view.isHidden = true
}
override open func viewDidAppear(_ animated: Bool) {
let appDirection = UIApplication.shared.statusBarOrientation
let deviceDirection = UIDevice.current.orientation
var direction:CGFloat = 0
if deviceDirection == .portrait {
if appDirection == .landscapeLeft {
direction = 1.0
} else if appDirection == .landscapeRight {
direction = -1.0
}
} else if deviceDirection == .portraitUpsideDown {
if deviceDirection == .landscapeLeft {
direction = -1.0
} else if appDirection == .landscapeRight {
direction = 1.0
}
}
if direction != 0 {
view.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2) * direction);
}
view.isHidden = false
}
open override func viewWillDisappear(_ animated: Bool) {
view.isHidden = true
}
}
如果您的應用程序支持landscapeRight,landscapeLeft和default(如肖像),您可以嘗試這樣做
self.present(alertController, animated: false, completion: {
switch UIDevice.current.orientation {
case .landscapeRight:
self.alertController.view.transform=CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 2))
case .landscapeLeft:
self.alertController.view.transform=CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
default:
self.alertController.view.transform=CGAffineTransform.identity
}
})
這將根據您的屏幕旋轉旋轉警報視圖,而不會出現任何UI動畫問題。
我受到 Nico Nierenberg 的回答的啟發,但用一些 Core Animation 邏輯對其進行了擴充。 這個額外的邏輯是使警報在其呈現和關閉期間旋轉所必需的 animation(Nico 的原始實現只是隱藏警報,這將給出與為animated
傳遞false
相同的外觀)。
我還添加了一個通知偵聽器,它在設備方向改變時調整旋轉。
此實現假定您的原始演示文稿僅是縱向的。 如果您呈現的 controller 可以在其他方向,您將需要修復radians
方法。
這是整個 class:
import UIKit
class RotatingAlertController : UIAlertController {
private static let animKey = "rotationHack"
private func radians(for orientation: UIDeviceOrientation) -> CGFloat {
switch orientation {
case .landscapeLeft: return CGFloat.pi*0.5
case .portraitUpsideDown: return CGFloat.pi*1
case .landscapeRight: return CGFloat.pi*1.5
default: return 0
}
}
private func applyCoreAnimationRotation() {
let rotationValue = radians(for: UIDevice.current.orientation)
let anim = CABasicAnimation(keyPath: "transform.rotation.z")
anim.duration = 9999
anim.toValue = rotationValue
anim.fromValue = rotationValue
view.layer.add(anim, forKey: RotatingAlertController.animKey)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
applyCoreAnimationRotation()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.layer.removeAnimation(forKey: RotatingAlertController.animKey)
view.transform = CGAffineTransform(rotationAngle: radians(for: UIDevice.current.orientation))
weak var weakSelf = self
NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification,
object: nil,
queue: nil) { _ in
guard let strongSelf = weakSelf else { return }
let orientation = UIDevice.current.orientation
guard orientation != .unknown &&
orientation != .faceDown &&
orientation != .faceUp
else {
return
}
UIView.animate(withDuration: 0.25, delay: 0) {
strongSelf.view.transform = CGAffineTransform(rotationAngle: strongSelf.radians(for: orientation))
}
}
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self)
applyCoreAnimationRotation()
}
}
您可以像這樣調用它:
let alert = RotatingAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.