如何让多个触摸按钮与 TouchBegan/Moved 一起使用?
我有一堆按钮,我想以三种不同的方式激活它们。
- Touch Down
- Touch Down - 多次触摸(同时)
- Touch Drag Inside(与在钢琴上拖动手指相同)
前两个在 IB 中显然很容易。然而,很多人,包括我自己,都在使用 Touch Drag 时遇到了麻烦。所以我最终使用 - (void) TouchMoved
[参见代码] 。这对于拖动来说非常有用...但是为了让它工作,我必须禁用 IB 中的“用户交互”按钮。这意味着我失去了“Touch Down”和多点触控功能。
因此,为了让“Touch Down”发挥作用,我使用了 -(void) TouchsBegan [参见代码]。这工作正常,但我无法让多点触控工作。
有谁知道如何让我的按钮在多点触控期间同时触发?
或者...有没有办法让IB中的触摸移动和按钮功能一起工作?
我已经尝试过 touch.view.multiTouchEnabled = Yes; 并确保我的按钮在 IB 中可以多次触摸...但什么也没有。
下面是我的代码。 非常感谢您的帮助。
-(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];
}
}
///
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(p1.frame, location))
{
[self pP01];
[p1 setHighlighted:YES];
}
if(CGRectContainsPoint(p2.frame, location))
{
[self pP02];
[p2 setHighlighted:YES];
}
if(CGRectContainsPoint(p3.frame, location))
{
[self pP03];
[p3 setHighlighted:YES];
}
}
I have a bunch of buttons that I want to activate in three different ways.
- Touch Down
- Touch Down - multiple touch (at the same time)
- Touch Drag Inside (The same as dragging your finger over a piano)
The first two is obviously easy in IB. However many people, including myself, have had trouble with Touch Drag inside. So I ended up using - (void) touchesMoved
[see code] . This works great for the drag... but to get it to work I had to disable the buttons "user Interaction" in IB. Which means I lost the "Touch Down" and multi-touch capabilities.
So, in order to get the "Touch Down" to work, I used -(void) touchesBegan [see code]. This works fine, but I can't get multi-touch to work.
Does anyone know how I can get my buttons to fire simultaneously during multi-touch?
Or ... Is there a way to get touches moved and button functions in IB to work together?
I have tried touch.view.multiTouchEnabled = Yes;
and I have ensured that my buttons are multiple touch ok in IB... But nothing.
Below is my code.
Thank you very much for your help.
-(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];
}
}
///
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(p1.frame, location))
{
[self pP01];
[p1 setHighlighted:YES];
}
if(CGRectContainsPoint(p2.frame, location))
{
[self pP02];
[p2 setHighlighted:YES];
}
if(CGRectContainsPoint(p3.frame, location))
{
[self pP03];
[p3 setHighlighted:YES];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要检查每次触摸,而不是随机触摸。因此,
for(UITouch *t in Touchs)
而不是UITouch *touch = [touches anyObject]
You need to check every touch instead of one random touch. So,
for(UITouch *t in touches)
instead ofUITouch *touch = [touches anyObject]