ios - Swift:移动 UIView

标签 ios swift uiviewanimation

我想为所有屏幕尺寸创建类似的动画。我的中心有 UIView,我想向右移动,UIView 和屏幕之间的距离应为 10。

怎么做?

我的一个屏幕代码(iPhone 5s):

UIView.animate(withDuration: 1.0, delay: 0.0, options: [], animations: {

   self.imageRevealed.center.x += 200

})

在这种情况下,UIView 和屏幕之间的距离 = 10。但对于其他屏幕,我应该使用新坐标编写新代码。如何编写通用代码?

最佳答案

我认为您需要 10px 的 ImageView 和 View 之间的尾随空间。

let trailingSpace: CGFloat = 10
UIView.animate(withDuration: 1.0, delay: 0.0, options: [], animations: {

   let screenSize = UIScreen.main.bounds.size

   let x = screenSize.width - self.imageRevealed.frame.size.width - trailingSpace
   let y = self.imageRevealed.frame.origin.y

   self.imageRevealed.frame = CGRect(x: x, y: y, width: self.imageRevealed.frame.size.width, height: self.imageRevealed.frame.size.height)

})

关于ios - Swift:移动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51112354/

相关文章:

ios - 如何停止和反转 UIView 动画?

ios - UIView 动画 - 动画正确但当再次到达相同位置时

ios - Firebase动态链接不会缩短URL

ios - 连接SignalR服务器的流程

ios - 如何在每个 View 上添加 UISearchbar

ios - hidesBarsOnSwipe 将意外显示工具栏

ios - UIview Flip 使 Views 变暗

ios - 虚拟机 : UITableViewLabel eating up memory iOS

ios - 在模拟器中而不是在设备上运行时进行条件编译

ios - 尝试使用插图隐藏 UITableViewCell 分隔符,但它只会降低不透明度