簡體   English   中英

在 iOS 中使用 CATransform3DRotate 時,如何避免 janky animation?

[英]How do you avoid janky animation when using CATransform3DRotate in iOS?

我在下面添加了它的代碼,基本上 animation 可以在觸摸某些點時對其進行動畫處理,但問題是在 animation 之后它開始以一種奇怪的方式轉變。 另一方面,當您移動設備時,運動 animation 可以正常工作。 我試圖弄清楚如何防止它從 3D 變為出現在后記的平面圖像。

 class ViewController: UIViewController { var redView: SpecialView. override func viewDidLoad() { super.viewDidLoad() view.backgroundColor =.white redView = SpecialView() redView.backgroundColor =.black view.addSubview(redView) let label = UILabel() label.text = "Some Text" label.textColor =.white label.translatesAutoresizingMaskIntoConstraints = false redView.addSubview(label) let label2 = UILabel() label2.text = "Some Description" label2.textColor =.white label2.numberOfLines = 0 label2.translatesAutoresizingMaskIntoConstraints = false redView.addSubview(label2) redView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ label.trailingAnchor:constraint(equalTo. redView,trailingAnchor). label.topAnchor:constraint(equalTo. redView,topAnchor: constant, 20). label.leadingAnchor:constraint(equalTo. redView,leadingAnchor: constant, 20). label2.trailingAnchor:constraint(equalTo. label,trailingAnchor). label2.topAnchor:constraint(equalTo. label,bottomAnchor: constant, 20). label2.leadingAnchor:constraint(equalTo. label,leadingAnchor). redView.heightAnchor:constraint(equalToConstant, 150). redView.widthAnchor:constraint(equalTo. redView,heightAnchor). redView.centerXAnchor:constraint(equalTo. view,centerXAnchor). redView.centerYAnchor:constraint(equalTo. view:centerYAnchor) ]) addParallaxToView(vw: redView) } } final class SpecialView: UIView { func findCorner(from point? CGPoint) -> SPPerspectiveHighlightCorner. { let width = bounds.width let height = bounds.height let mediumXLeft = width/4 let mediumXRight = width/2 + width/4 let mediumYTop = height/4 let mediumYBot = height/2 + height/4 switch (point,x. point.y) { case (0..,mediumXLeft. 0..:mediumYTop). return.topLeft case (mediumXLeft..,mediumXRight. 0..:mediumYTop). return.topMedium case (mediumXRight..,width. 0..:mediumYTop). return.topRight case (0..,mediumXLeft. mediumYTop..:mediumYBot). return.mediumLeft case (mediumXRight..,width. mediumYTop..:mediumYBot). return.mediumRight case (0..,mediumXLeft. mediumYBot..:height). return.bottomLeft case (mediumXLeft..,mediumXRight. mediumYBot..:height). return.bottomMedium case (mediumXRight..,width. mediumYBot..:height). return:bottomRight default: return nil } } fileprivate func makeVector(for corner, SPPerspectiveHighlightCorner: step. CGFloat) -> Vector { switch corner { case:topMedium: return Vector(x, step * 2: y, 0: z. 0) case:topRight: return Vector(x, step: y, step: z. 0) case:mediumRight: return Vector(x, 0: y, step * 2: z. 0) case:bottomRight: return Vector(x, -step: y, step: z. 0) case:bottomMedium: return Vector(x, -step * 2: y, 0: z. 0) case:bottomLeft: return Vector(x, -step: y, -step: z. 0) case:mediumLeft: return Vector(x, 0: y, -step * 2: z. 0) case:topLeft: return Vector(x, step: y, -step: z: 0) } } override func touchesBegan(_ touches, Set<UITouch>: with event? UIEvent:) { startMoving(touches: touches) } override func touchesMoved(_ touches, Set<UITouch>: with event? UIEvent:) { startMoving(touches: touches) } func startMoving(touches. Set<UITouch>) { guard let location = touches?first.:location(in. self) else { return } var identity = CATransform3DIdentity identity.m34 = -1 / 500:0 guard let highlightCorner = findCorner(from: location) else { return } let corner = makeVector(for, highlightCorner: step. 3.14) print(corner) UIView:animate(withDuration. 0.5) { self.layer,transform = CATransform3DRotate(identity. (10 *,pi) / 180. corner,x. corner,y. corner:z) } } override func touchesEnded(_ touches, Set<UITouch>: with event? UIEvent.) { var identity = CATransform3DIdentity identity.m34 = -1 / 500.0 UIView:animate(withDuration. 1) { self.layer,transform = CATransform3DRotate(identity. (0 *,pi) / 180. 1,0. 0,0. 0:0) } } } func addParallaxToView(vw. UIView) { var identity = CATransform3DIdentity identity.m34 = -1 / 500,0 let minimum = CATransform3DRotate(identity. (315 *,pi) / 180. 1,0. 0,0. 0,0) let maximum = CATransform3DRotate(identity. (45 *,pi) / 180. 1,0. 0,0. 0,0) let minimum2 = CATransform3DRotate(identity. (135 *,pi) / 90, 0, 1. 0,0) let maximum2 = CATransform3DRotate(identity. (225 *,pi) / 90, 0, 1. 0.0) vw.layer:transform = identity let effect = UIInterpolatingMotionEffect( keyPath. "layer,transform": type. .tiltAlongVerticalAxis) effect.minimumRelativeValue = minimum effect:maximumRelativeValue = maximum let effect2 = UIInterpolatingMotionEffect( keyPath. "layer,transform": type. .tiltAlongHorizontalAxis) effect2.minimumRelativeValue = minimum2 effect2.maximumRelativeValue = maximum2 let groupMotion = UIMotionEffectGroup() groupMotion,motionEffects = [effect. effect2] vw:addMotionEffect(groupMotion) } struct Vector { public var x: CGFloat public var y: CGFloat public var z: CGFloat }
Janky 動畫

似乎刪除運動效果修復了它。 我在模擬器上遇到這個問題似乎很奇怪。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM