touchesBegin & touchesMove Xcode Obj C Question

纵饮孤独 提交于 2020-01-11 04:00:24

问题


So I have my app working good when you press and drag along. I also have UIButtons set to Touch Down in Interface Builder.

As well when you drag you need to drag from the outside of the UIButton. You cannot click on the UIButton and drag to the other.

TOUCHES MOVED:

Code:

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

UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];

if(CGRectContainsPoint(oneButton.frame, location))
{
    if (!oneButton.isHighlighted){
        [self oneFunction];
        [oneButton setHighlighted:YES];
    }
}else {
    [oneButton setHighlighted:NO];
}
//
if(CGRectContainsPoint(twoButton.frame, location)) 
{
    if (!twoButton.isHighlighted){
        [self twoFunction];
        [twoButton setHighlighted:YES];
    }
}else {
    [twoButton setHighlighted:NO];
}

}

TOUCHES BEGAN:

Code:

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

   UITouch *touch = [[event touchesForView:self.view] anyObject];

CGPoint location = [touch locationInView:touch.view];

if(CGRectContainsPoint(oneButton.frame, location))
{
    [self oneFunction];
    [oneButton setHighlighted:YES];
}
if(CGRectContainsPoint(twoButton.frame, location))
{
    [self twoFunction];
    [twoButton setHighlighted:YES];
}

}

I want to be able to click on any of the button fire the function & also be able to drag from one button on to the other and fire that function.

So basically just being able to click on a button and slide your finger over and activate the other button without having to press and slide from outside of the button.

I think I'm close, need a bit of help. Hope thats clear enough.

Thanks.


回答1:


Tried something different. Using this worked much better:

if(CGRectContainsPoint(myObject.frame, location) && lastButton != myObject) {

Used it in touchesBegan & touchesMoved.



来源:https://stackoverflow.com/questions/3050184/touchesbegin-touchesmove-xcode-obj-c-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!