简体   繁体   English

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

[英]How to show highlight properly for two images using multi-touch?

I have problem with custom double slider. 我对自定义双滑块有疑问。 There are two images which are thumbs. 有两个图像是大拇指。 Application works fine, both thumbs can move using both fingers, but there are problem with highlight . 应用程序工作正常,两只拇指都可以用两只手指移动,但是高亮显示存在问题。

When I take off finger from one thumb, highlight from second thumb also back to normal. 当我从一个拇指上取下手指时,从第二个拇指上突出显示也恢复正常。 When I'm using any if and else statment, it brings another bad results: sometimes highlight not changes or stay when finger move out from image. 当我使用任何ifelse陈述时,它带来另一个不好的结果:当手指从图像中移出时,有时高亮显示没有变化或停留。 Or blinks. 或眨眼。

I have no idea, how to continue work, to bind highlight with endTouches event properly. 我不知道如何继续工作,以正确地将高与endTouches事件绑定。 To not highlight thumbs, if not pressed. 如果未按下,则不突出拇指。 How to solve to? 怎么解决?

-(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];
    }
}

I think the issue is not on the touchesEnded , but on touchesMoved method. 我认为问题不是在touchesEnded ,而是在touchesMoved方法上。

You should implement the logic back into touchesEnded: 您应该将逻辑重新实现为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;
        }
    }
}

and also make touchesMoved handle setting NO on the highlight: 并在高亮处将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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM