【发布时间】:2011-04-30 09:19:53
【问题描述】:
我正在几个 UIButton 上实现 touchesMoved、touchesBegan 和 touchesEnded,以便我可以将手指滑过它们并让它们调用适当的操作。
它似乎几乎按预期工作,但是,如果我将两个手指按在两个按钮的框架之外,然后同时将它们滑入按钮的框架中,则 touchesMoved 中的函数会被多次调用。相反,它应该只在按钮的框架中调用每个按钮的函数一次。
下面是我的代码。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for(UITouch *t in touches) {
CGPoint location = [t locationInView:t.view];
if(CGRectContainsPoint(Button1.frame, location))
{
if (!Button1.isHighlighted){
if(!button1Highlighted) {
[self doAction1];
}
[Button1 setHighlighted:YES];
button1Highlighted = YES;
}
}
else {
[Button1 setHighlighted:NO];
button1Highlighted = NO;
}
if(CGRectContainsPoint(Button2.frame, location))
{
if (!Button2.isHighlighted){
if(!button2Highlighted) {
[self doAction2];
}
[Button2 setHighlighted:YES];
button2Highlighted = YES;
}
}
else {
[Button2 setHighlighted:NO];
button2Highlighted = NO;
}
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *t in touches) {
CGPoint location = [t locationInView:t.view];
if(CGRectContainsPoint(Button1.frame, location))
{
[Button1 setHighlighted:YES];
button1Highlighted = YES;
[self doAction1];
}
if(CGRectContainsPoint(Button2.frame, location))
{
[Button2 setHighlighted:YES];
button2Highlighted = YES;
[self doAction2];
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *t in touches) {
CGPoint location = [t locationInView:t.view];
if(CGRectContainsPoint(Button1.frame, location))
{
[Button1 setHighlighted:NO];
button1Highlighted = NO;
}
if(CGRectContainsPoint(Button2.frame, location))
{
[Button2 setHighlighted:NO];
Button2Highlighted = NO;
}
}
}
非常感谢任何帮助。谢谢!
【问题讨论】:
标签: iphone objective-c