我有多个UIImageView
,分布在一个视图中。
@IBOutlet var leftIVs: [UIImageView]!
@IBOutlet var topIVs: [UIImageView]!
@IBOutlet var rightIVs: [UIImageView]!
我正在尝试使用UIView.animate...
函数和'transform'属性创建一个函数,该函数将所有这些UIImageView
置于superview
的中心。我正在使用以下代码:
let performInitialTransformation: ((UIImageView)->()) = { (card) in
let cardCenter = CGPoint(x: card.frame.midX, y: card.frame.midY)
let viewCenter = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
let deltaPoint = cardCenter - viewCenter //also tried (viewCenter - cardCenter)
UIView.animate(withDuration: 0.5, animations: {
card.transform = CGAffineTransform.init(translationX: deltaPoint.x, y: deltaPoint.y)
}) { (done) in
}
}
for card in topIVs {
performInitialTransformation(card)
}
for card in leftIVs {
performInitialTransformation(card)
}
for card in rightIVs {
performInitialTransformation(card)
}
我正在使用此静态函数:
extension CGPoint {
static func -(lhs: CGPoint, rhs: CGPoint) -> CGPoint {
return CGPoint(x: rhs.x - lhs.x, y: rhs.y - lhs.y)
}
}
[[[注意:另外,我将这些图像随后放回原始位置,我将使用CGAffineTransform.identity
center
将使用什么UIView.animate(withDuration: 0.5) {
images.forEach { $0.center = superview.center }
}
完整示例
var subviewes = [UIView]() var view = UIView(frame: CGRect.init(x: 0, y: 0, width: 30, height: 30)) view.backgroundColor = .yellow subviewes.append(view) view = UIView(frame: CGRect.init(x: 0, y: 0, width: 20, height: 20)) view.backgroundColor = .green subviewes.append(view) view = UIView(frame: CGRect.init(x: 0, y: 0, width: 10, height: 10)) view.backgroundColor = .red subviewes.append(view) subviewes.forEach { self.view.addSubview($0) } UIView.animate(withDuration: 0.5) { subviewes.forEach { $0.center = self.view.center } }