//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
label.shake(count: 0)
}
}
extension UIView {
func shake(completion: (() -> Void)? = nil, count: Int?) {
let duration: TimeInterval = 10
var animationDelays: [CGFloat] = []
let shakeAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.5)
var xPos: [CGFloat] = [-100,0,100,0]
var yPos: [CGFloat] = [0,0,0,0]
guard xPos.count == yPos.count else {
return
}
for i in 0..<xPos.count {
xPos[i] = xPos[i]
yPos[i] = yPos[i]
animationDelays.append( CGFloat(i) * (1.0 / CGFloat(xPos.count) ))
}
for i in 0..<xPos.count {
print("animation added")
shakeAnimator.addAnimations({
self.transform = CGAffineTransform(translationX: xPos[i], y: yPos[i])
}, delayFactor: animationDelays[i])
}
shakeAnimator.addCompletion { _ in
print("animation finished!")
}
shakeAnimator.startAnimation()
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
'ios개발' 카테고리의 다른 글
Swift - Getters and Setters 스위프트 get, set (0) | 2021.07.24 |
---|---|
Swift - Access Control, 스위프트 제한자(open, public, internal, fileprivate, private) (0) | 2021.07.24 |
<iOS 디버깅> app store connect 배포 에러 ITMS-90713 (0) | 2021.05.11 |
swift에서 @objc란? (0) | 2021.05.01 |
<iOS개발> swift에서 sender란? (0) | 2021.05.01 |