如何使用多点触控正确显示两个图像的高亮显示?

时间:2015-05-18 20:21:08

标签: ios multi-touch uislider

我有自定义双滑块的问题。有两个图像是拇指。应用程序工作正常,两个拇指都可以用双手移动,但突出显示有问题。

当我从一个拇指上取下手指时,从第二个拇指突出显示也恢复正常。当我使用任何ifelse声明时,它会带来另一个不好的结果:有时突出显示不会更改,或者当手指从图像移出时停留。或者眨眼。

我不知道如何继续工作,正确地将突出显示与endTouches事件绑定在一起。如果没有按下,则不突出显示拇指。怎么解决?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

   for(UITouch *touch in [event allTouches]) {

       CGPoint touchPoint = [touch locationInView:self];
       if(CGRectContainsPoint(thumbLeft.frame, touchPoint)){
           thumbLeft.highlighted = YES;
       }
       if(CGRectContainsPoint(thumbRight.frame, touchPoint)){
           thumbRight.highlighted = YES;
       }

    }
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    //No idea here
    for(UITouch *touch in [event allTouches]) {
        CGPoint touchPoint = [touch locationInView:self];


        //----------- ???? ----------//

        thumbRight.highlighted = NO;
        thumbLeft.highlighted = NO;

    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    for(UITouch *touch in [event allTouches]) {

        CGPoint touchPoint = [touch locationInView:self];
        if (CGRectContainsPoint(thumbLeft.frame, touchPoint)){
            thumbLeft.center = CGPointMake(MAX(barLeftHorizontalPosition, MIN(touchPoint.x, thumbRight.center.x - minimumRange)), thumbLeft.center.y);
            thumbLeft.highlighted = YES;
            valueLeft = [self valueGetX:thumbLeft.center.x];
        }

        if (CGRectContainsPoint(thumbRight.frame, touchPoint)){
            thumbRight.center = CGPointMake(MAX(thumbLeft.center.x + minimumRange, MIN(touchPoint.x, barRightHorizontalPosition)), thumbRight.center.y);
            thumbRight.highlighted = YES;
            valueRight = [self valueGetX:thumbRight.center.x];
        }

        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题不在touchesEnded上,而是在touchesMoved方法上。

你应该将逻辑重新实现为touchesEnded:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    for(UITouch *touch in [event allTouches]) {
        CGPoint touchPoint = [touch locationInView:self];
        if(CGRectContainsPoint(thumbLeft.frame, touchPoint)){
            thumbLeft.highlighted = NO;
        }
        if(CGRectContainsPoint(thumbRight.frame, touchPoint)){
            thumbRight.highlighted = NO;
        }
    }
}

并使touchesMoved处理亮点设置为NO:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    for(UITouch *touch in [event allTouches]) {
        CGPoint touchPoint = [touch locationInView:self];

        if (CGRectContainsPoint(thumbLeft.frame, touchPoint)){
            thumbLeft.center = CGPointMake(MAX(barLeftHorizontalPosition, MIN(touchPoint.x, thumbRight.center.x - minimumRange)), thumbLeft.center.y);
            thumbLeft.highlighted = YES;
            valueLeft = [self valueGetX:thumbLeft.center.x];
        } else {
            thumbLeft.highlighted = NO;
        }

        if (CGRectContainsPoint(thumbRight.frame, touchPoint)){
            thumbRight.center = CGPointMake(MAX(thumbLeft.center.x + minimumRange, MIN(touchPoint.x, barRightHorizontalPosition)), thumbRight.center.y);
            thumbRight.highlighted = YES;
            valueRight = [self valueGetX:thumbRight.center.x];
        } else {
            thumbRight.highlighted = NO;
        }

        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}