view 移動

var gandieImageView: UIImageView? override func viewWillAppear(_ animated: Bool) { ... updateAnimation() } override func viewDidDisappear(_ animated: Bool) { gandieImageView?.image = nil } private func updateAnimation() { let screenWidth = UIScreen.main.bounds.size.width let screenHeight = UIScreen.main.bounds.size.height let iconWidth: CGFloat = 82 // Fixed value by image size let iconHeight: CGFloat = 81 // Fixed value by image size let containerHeight: CGFloat = 64 // Fixed value by auto layout //var topSafeAreaHeight: CGFloat = 0 var bottomSafeAreaHeight: CGFloat = 0 if #available(iOS 11.0, *) { let window = UIApplication.shared.windows[0] let safeFrame = window.safeAreaLayoutGuide.layoutFrame //topSafeAreaHeight = safeFrame.minY // 取得top safe area高度 bottomSafeAreaHeight = window.frame.maxY - safeFrame.maxY // 取得 bottom safe area高度 } let images = [UIImage(named: "move01")!, UIImage(named: "move02")!, UIImage(named: "move03")!, UIImage(named: "move04")!] let animatedImage = UIImage.animatedImage(with: images, duration: 1) // 建立UIImageView並設定初始位置 gandieImageView = UIImageView(frame: CGRect( x: screenWidth + iconWidth, y: screenHeight - bottomSafeAreaHeight - containerHeight - iconHeight - 5, width: iconWidth, height: iconHeight) ) gandieImageView?.image = animatedImage gandieImageView?.contentMode = .scaleAspectFit // 要移到的位置 UIView.animate(withDuration: 1) { self.gandieImageView?.frame = CGRect( x: screenWidth - iconWidth - 5, y: screenHeight - bottomSafeAreaHeight - containerHeight - iconHeight - 5, width: iconWidth, height: iconHeight ) } self.view.addSubview(gandieImageView!) addGesture(imageView: gandieImageView) }
tags: 實作相關