我试图在用户触摸并按住按钮时制作动画,当释放触摸时,动画停止。怎么做?
试试这个
为您的按钮添加 TouchDown、Touch Up Inside、Touch Up Outside 事件
-(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
}
尝试实现以下方法
-(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()
}
您有 UIButton 的
Touch Down
和 Touch Up Inside
和 Touch Drag Outside
功能。
创建两个用于开始和结束动画的方法。将开始动画方法映射到
Touch Down
,将停止动画方法映射到 Touch Up Inside
和 Touch Drag Outside
发送事件。
因为用户可以将手指从 UIButton 拖到外面。它会工作得很好。