[Swift] 클릭하면 커지고 나가면 멍 때리는 UIButton의 부반.
컨디션
Xcode9.4.1
Swift4
이미지
이루어지다 class ExpandButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit() {
self.addTarget(self, action: #selector(self.touchDownAnimation), for: .touchDown)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchUpInside)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchUpOutside)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchCancel)
}
@objc
func touchDownAnimation() {
let scale: CGFloat = (round(UIScreen.main.bounds.width / self.bounds.width / 2) / 10) + 1
UIView.animate(
withDuration: 0.1,
delay: 0,
options: .curveLinear,
animations: {
self.transform = self.transform.scaledBy(x: scale, y: scale)
}, completion: nil)
}
@objc
func touchUpAnimation() {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 10,
options: .curveEaseOut,
animations: {
self.transform = .identity
}, completion: nil)
}
}
설명 같은 거.
이루어지다 class ExpandButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit() {
self.addTarget(self, action: #selector(self.touchDownAnimation), for: .touchDown)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchUpInside)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchUpOutside)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchCancel)
}
@objc
func touchDownAnimation() {
let scale: CGFloat = (round(UIScreen.main.bounds.width / self.bounds.width / 2) / 10) + 1
UIView.animate(
withDuration: 0.1,
delay: 0,
options: .curveLinear,
animations: {
self.transform = self.transform.scaledBy(x: scale, y: scale)
}, completion: nil)
}
@objc
func touchUpAnimation() {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 10,
options: .curveEaseOut,
animations: {
self.transform = .identity
}, completion: nil)
}
}
설명 같은 거.
class ExpandButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit() {
self.addTarget(self, action: #selector(self.touchDownAnimation), for: .touchDown)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchUpInside)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchUpOutside)
self.addTarget(self, action: #selector(self.touchUpAnimation), for: .touchCancel)
}
@objc
func touchDownAnimation() {
let scale: CGFloat = (round(UIScreen.main.bounds.width / self.bounds.width / 2) / 10) + 1
UIView.animate(
withDuration: 0.1,
delay: 0,
options: .curveLinear,
animations: {
self.transform = self.transform.scaledBy(x: scale, y: scale)
}, completion: nil)
}
@objc
func touchUpAnimation() {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 10,
options: .curveEaseOut,
animations: {
self.transform = .identity
}, completion: nil)
}
}
let scale: CGFloat = (round(UIScreen.main.bounds.width / self.bounds.width / 2) / 10) + 1
이것은 단추의 크기에 따라 확대율을 적당히 조정한다.작은 버튼이면 큰 버튼이면 작아진다.왜냐하면 같은 배율로 확대하면 이상해!(이 + 50pt 같은 것은 확대를 지정하는 것이 좋습니다...)Reference
이 문제에 관하여([Swift] 클릭하면 커지고 나가면 멍 때리는 UIButton의 부반.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/rd0501/items/6475edeaccfc08b2af4f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)