UIButton触摸并按住做动画,松开时动画停止[关闭]

问题描述 投票:0回答:3

我试图在用户触摸并按住按钮时制作动画,当释放触摸时,动画停止。怎么做?

ios iphone animation uibutton
3个回答
2
投票

试试这个

为您的按钮添加 TouchDown、Touch Up Inside、Touch Up Outside 事件

enter image description here

-(IBAction)theTouchDown:(id)sender
{

[self startAnimation];

}
-(IBAction)theTouchUpInside:(id)sender
{

[self stopAnimation];

}
-(IBAction)theTouchUpOutside:(id)sender
{

[self stopAnimation];

}

-(void)startAnimation
 {
 //write your logic
 }

-(void)stopAnimation
 {
 //write your logic
 }

2
投票

尝试实现以下方法

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Start your animation
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   // stop your animation
}

迅捷

按钮或视图

view.isUserInteractionEnabled = true

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    self.tapAnimateBegan()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    self.tapAnimateEnded()
}

1
投票

您有 UIButton 的

Touch Down
Touch Up Inside
Touch Drag Outside
功能。

创建两个用于开始和结束动画的方法。将开始动画方法映射到

Touch Down
,将停止动画方法映射到
Touch Up Inside
Touch Drag Outside
发送事件。

因为用户可以将手指从 UIButton 拖到外面。它会工作得很好。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.