问题描述
是否可以将 touchesMoved
函数与按钮一起使用而不是 UIImageView
s?
Is it possible to use the touchesMoved
function with buttons instead of UIImageView
s?
推荐答案
是的.
在您的 .h 文件中
IBOutlet UIButton *aButton;
在您的 .m 文件中
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(p1.frame, location))
{
if (!p1.isHighlighted){
[self pP01];
[p1 setHighlighted:YES];
}
}else {
[p1 setHighlighted:NO];
}
//
if(CGRectContainsPoint(p2.frame, location))
{
if (!p2.isHighlighted){
[self pP02];
[p2 setHighlighted:YES];
}
}else {
[p2 setHighlighted:NO];
}
if(CGRectContainsPoint(p3.frame, location))
{
if (!p3.isHighlighted){
[self pP03];
[p3 setHighlighted:YES];
}
}else {
[p3 setHighlighted:NO];
}
}
最后,在 Interface Builder 中,将您的按钮连接到aButton"并为您的按钮关闭启用用户交互".这很重要,因为它允许 touchesMoved 处理它.
And finally, in Interface Builder, connect your button to 'aButton' and turn off "User Interaction Enabled" for your button. This is important because it allow touchesMoved to handle it.
我已经调整了上面的代码来检查按钮的高亮状态.这是为了在您在该区域内拖动手指时阻止它多次触发按钮.
I have adjusted the code above to check the buttons highlighted state. This is to stop it from firing the button multiple times when you drag your finger inside the area.
要让您的钢琴键"在您敲击时发挥作用,请使用 -(void)touchesBegan
To get your "piano keys" to work when you tap them, use -(void)touchesBegan
要将按钮高亮状态设置回 = NO;
,请使用 -(void)touchesEnded
To set your button highlight states back to = NO;
, use -(void)touchesEnded
我不得不找出你所追求的完全相同的东西.我想不通 Touch Drag Enter
I have had to find out the exact same thing you are after. I couldn't figure out Touch Drag Enter
因此,为避免在该主题上发布多个帖子,请查看我的问题和答案.
So to avoid multiple posts on the subject, please check out my question and answers.
问题1
问题2
这篇关于TouchesMoved 与 Button?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!