2014-11-26 1 views
1

UIButton에 대한 하위 클래스를 만들었으므로 탭에서 애니메이션 효과를 얻을 수있었습니다. 효과가 작동하지 않습니다. 작동하지 않는 것은 시도를 할 때 @IBAction 뷰 컨트롤러. 함수가 호출되지 않습니다. 내 점수 판에 하위 클래스를 단추 클래스로 설정했습니다. 나는이 원인을 모르겠어요 어떤 아이디어UIButton Subclass @IBAction이 작동하지 않음

@IBAction func test(sender: AnyObject) { 

    println("go") 
} 

:

import UIKit 

class SpringyButton: UIButton { 

    /* 
    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func drawRect(rect: CGRect) { 
     // Drawing code 
    } 
    */ 

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 

     UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { 


      self.layer.transform = CATransform3DMakeScale(0.8, 0.8, 1) 


      }, completion: nil) 

    } 

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { 


     UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { 


      self.layer.transform = CATransform3DMakeScale(1, 1, 1) 


      }, completion: nil) 

    } 

    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) { 


     UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { 


      self.layer.transform = CATransform3DMakeScale(1, 1, 1) 


      }, completion: nil) 

    } 

    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) { 


     UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { 


      self.layer.transform = CATransform3DMakeScale(1, 1, 1) 


      }, completion: nil) 
    } 


} 

내의 ViewController 내 IBAction를 : 여기에 내 코드입니까?

사이드 노트 : UIButton을 서브 클래 싱하는 것은 그리 좋은 방법이 아니므로 UIView를 서브 클래스 화하고 탭 제스처 인식기로 연결하는 것이 더 낫겠습니까?

감사합니다.

+0

이 작업을 수행하려면 UIButton을 하위 클래스로 만들지 마십시오. 나는 그것을 행동이나 뭔가의 일부로 추가 할뿐입니다. – Fogmeister

+0

@Fogmeister가 시도한대로, 애니메이션이 동작으로 올바르게 작동하지 않습니다. – mlevi

+0

정상 동작으로 절대 수행 할 수 있습니다 : D 'touchUpInside','touchDownInside' 등에 대한 동작을 추가하기 만하면됩니다 ... BTW, I 버튼의 레이어가 아닌 버튼 자체를 변형합니다. – Fogmeister

답변

1

문제는 터치 이벤트를 무시하는 것입니다. 함수를 재정의 한 후에 super를 호출해야합니다. 아래 touchesBegan 기능의 예를 들면 다음과 같은

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 

    UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { 

      self.layer.transform = CATransform3DMakeScale(0.8, 0.8, 1) 


    }, completion: nil) 
    super.touchesBegan(touches, withEvent: event) 
} 

당신은 당신이 우선 터치 이벤트의 모든 초를 추가해야합니다. 문제가 발생하면 알려주십시오.

0

인터페이스 작성기는 UIButton 만 사용한다고 생각합니다. 그래서 하위 클래스 화는 실제로 작동하지 않습니다. 사용자 지정 클래스를 실제로 사용하려는 경우 프로그래밍 방식으로 만들 수 있습니다.

let myButton : SpringyButton = SpringyButton() 

내가 작동하는지 그리고/또는 그것이 당신이 찾고있는 것이라면 알려주세요!

관련 문제