iphone开发:在内部同时使用触控和触控

iphone开发:在内部同时使用触控和触控,iphone,objective-c,ios,cocoa-touch,user-interaction,Iphone,Objective C,Ios,Cocoa Touch,User Interaction,在我的应用程序中,我使用了一个按钮,并为它们指定了两种方法:一种是在触摸时工作(按钮图像已更改),另一种是在触摸内部时工作(打开另一个视图)。因此,简单地说,如果你想打开一个视图,你按下按钮,但当你触摸按钮时,图像会改变,当你抬起手指后,另一个视图会打开。我的问题是,如果你按下按钮,图像就会改变,但是如果你把手指移到远离按钮的地方,内部的润色就不能正常工作。但问题是,由于触地是一次触发的,所以图像会与其过度版本保持一致。我该怎么办?谢谢您可以在控制状态下处理此问题,具体取决于您希望它执行的操作。

在我的应用程序中,我使用了一个按钮,并为它们指定了两种方法:一种是在触摸时工作(按钮图像已更改),另一种是在触摸内部时工作(打开另一个视图)。因此,简单地说,如果你想打开一个视图,你按下按钮,但当你触摸按钮时,图像会改变,当你抬起手指后,另一个视图会打开。我的问题是,如果你按下按钮,图像就会改变,但是如果你把手指移到远离按钮的地方,内部的润色就不能正常工作。但问题是,由于触地是一次触发的,所以图像会与其过度版本保持一致。我该怎么办?谢谢

您可以在控制状态下处理此问题,具体取决于您希望它执行的操作。使用
touchDragOutside
可以检测用户何时触到按钮内部并将手指拖离而不离开按钮的可触边界,
touchDragExit
可以检测用户何时将手指拖离按钮可触边界

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragOutside];

您可以在控件状态
touchDragOutside
touchDragExit
中处理此问题,具体取决于您希望它执行的操作。使用
touchDragOutside
可以检测用户何时触到按钮内部并将手指拖离而不离开按钮的可触边界,
touchDragExit
可以检测用户何时将手指拖离按钮可触边界

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragOutside];

我建议您使用UIButton对象的这种方法来更改图像

- (void)setImage:(UIImage *)image forState:(UIControlState)state
你可以在这里看到该州的所有选项


我会为您的目标使用UIControlStateNormal和UIControlStateHighlighted状态。

我建议您使用UIButton对象的此方法来更改图像

- (void)setImage:(UIImage *)image forState:(UIControlState)state
你可以在这里看到该州的所有选项


我会为您的目标使用uicontrol状态正常和uicontrol状态突出显示。

我自己也遇到过这个问题,我们主要使用这些事件:-

//此事件运行良好,并引发火灾

[按钮添加目标:自我操作:@selector(holdDown)for ControlEvents:UIControlEventTouchDown]

//这根本不着火

[按钮添加目标:自我操作:@selector(holdRelease)for ControlEvents:UIControlEventTouchUpInside]

解决方案:-

使用长按手势识别器:-

 UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)];
[button addGestureRecognizer:btn_LongPress_gesture];
手势的实施:

- (void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{


//as you hold the button this would fire

if (recognizer.state == UIGestureRecognizerStateBegan) {

    [self someMethod];
}

//as you release the button this would fire

if (recognizer.state == UIGestureRecognizerStateEnded) {

    [self someMethod];
}
}

我自己也遇到过这个问题,我们主要使用这些事件:-

//此事件运行良好,并引发火灾

[按钮添加目标:自我操作:@selector(holdDown)for ControlEvents:UIControlEventTouchDown]

//这根本不着火

[按钮添加目标:自我操作:@selector(holdRelease)for ControlEvents:UIControlEventTouchUpInside]

解决方案:-

使用长按手势识别器:-

 UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)];
[button addGestureRecognizer:btn_LongPress_gesture];
手势的实施:

- (void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{


//as you hold the button this would fire

if (recognizer.state == UIGestureRecognizerStateBegan) {

    [self someMethod];
}

//as you release the button this would fire

if (recognizer.state == UIGestureRecognizerStateEnded) {

    [self someMethod];
}
}