Skip to content

Instantly share code, notes, and snippets.

View marekkrzynowek's full-sized avatar

marekkrzynowek

  • Montrose Software LLC
  • Kraków
View GitHub Profile
let bigSquare = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 120))
bigSquare.backgroundColor = UIColor.green
let smallSquare = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
bigSquare.addSubview(smallSquare)
smallSquare.backgroundColor = UIColor.blue
smallSquare.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
smallSquare.frame = CGRect(x: 20, y: 20, width: 80, height: 80)
UIView.animate(withDuration: 1) {
smallSquare.layer.position = CGPoint(x: 60, y: 60)
}
@marekkrzynowek
marekkrzynowek / why.swift
Last active October 11, 2017 12:58
UIView frame animations with scaling and position
UIView.animate(withDuration: 1) {
myView.frame = CGRect(x: 60, y: 60, width: 100, height: 100)
}
UIView.animate(withDuration: 1) {
smallSquare.layer.position = CGPoint(x: 60, y: 60)
smallSquare.transform = CGAffineTransform.identity.scaledBy(x: 2, y: 2)
}
func animateTo(frame: CGRect, withDuration duration: TimeInterval, completion: ((Bool) -> Void)? = nil) {
guard let _ = superview else {
return
}
let xScale = frame.size.width / self.frame.size.width
let yScale = frame.size.height / self.frame.size.height
let x = frame.origin.x + (self.frame.width * xScale)/2
let y = frame.origin.y + (self.frame.height * yScale)/2
UIView.animate(withDuration: duration, delay: 0, options: .curveLinear, animations: {
smallSquare.animateTo(frame: CGRect(x: 60, y: 60, width: 80, height: 80), withDuration: 1)
let x = frame.origin.x + (self.frame.width * xScale)/2
let y = frame.origin.y + (self.frame.height * yScale)/2
let x = frame.origin.x + (self.frame.width * xScale) * self.layer.anchorPoint.x
let y = frame.origin.y + (self.frame.height * yScale) * self.layer.anchorPoint.y