[Swift] 클릭하면 커지고 나가면 멍 때리는 UIButton의 부반.

7688 단어 UIButtonSwift
저는 github가 더 좋은 게 있다고 생각해요.

컨디션


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)
    }
}

설명 같은 거.

  • 버튼 색상 변화가 싫은 경우 스타일을 커스텀으로 바꾸는 건 괜찮을 것 같아요!
  • let scale: CGFloat = (round(UIScreen.main.bounds.width / self.bounds.width / 2) / 10) + 1 이것은 단추의 크기에 따라 확대율을 적당히 조정한다.작은 버튼이면 큰 버튼이면 작아진다.왜냐하면 같은 배율로 확대하면 이상해!(이 + 50pt 같은 것은 확대를 지정하는 것이 좋습니다...)
  • 좋은 웹페이지 즐겨찾기