The company project needs to add a gesture for a view, short press to disappear, long press to save to the album, in order to
Touchesended in the long press and short press started Google and Baidu, some people in Baidu said that can be achieved by the following ways:
- (void) touchesended: (nsset *) Touches withevent: (UIEvent *) event{ uitouch *atouch = [touches anyobject]; for (int i = 0; i < [atouch.gesturerecognizers count] ;i ++) { uigesturerecognizer *gesture = [ Atouch.gesturerecognizers objectatindex:i]; if ([Gesture iskindofclass:[uilongpressgesturerecognizer class]]) { //do what you want to do... } }}
After verification found that ATouch.gestureRecognizers.count is 0, can not judge whether long press, helpless had to add uilongpressgesturerecognizer and uitapgesturerecognizer gestures for the view, work can be realized, but the heart is still uncomfortable, I think iOS should not make such a low-level error, incredibly in the touchesended can not distinguish between long press short press, heart plug AH ~
Well, to get rid of the gloom in my heart, continue to study, print long press and short press touch info
Short Press:
<UITouch:0x1703801a0> phase:ended tap count:1 window: <UIWindow:0x156d467c0; frame = (0 0; 414 736); AutoResize = w+h; Gesturerecognizers = <NSArray:0x17024b280>; Layer = <UIWindowLayer:0x170233aa0>> View: <UIView:0x156d83810; frame = (76.6667 216.667; 261 303); Layer = <CALayer:0x170234d40>> location in window: {198.66667175292969, 332} Previous at window: {197, 332.66665649414062} location in view: {122.000005086263, 115.33333333333337} previous location in view: { 120.33333333333331, 115.999989827474}
Long press:
<uitouch: 0x17018f220> phase: ended tap count: 0 window: <uiwindow: 0x14e661ee0; frame = (0 0; 414 736); autoresize = w+h; gesturerecognizers = <nsarray: 0x17005eed0 >; layer = <UIWindowLayer: 0x17003e700>> view: <UIView: 0x14e5a2cb0; frame = (76.6667 216.667; 261 303); layer = < calayer: 0x1744329e0>> location in window: {184.66667175292969, 454.66665649414062} previous location in window: {188.33332824707031, 455.33334350585938} location in view: {108.000005086263, 237.999989827474} previous location in view: {111.66666158040363, 238.66667683919275}
Suddenly found that tap count is different, and then look at the Apple documentation, and so on, so there is the following relaxed and pleasant code:
-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{Uitouch *atouch = [touches anyobject]; Nsinteger tapcount = Atouch.tapcount; if (1 = = Tapcount) {//short press [self removefromsuperview]; } else if (0 = = Tapcount) {//long press UIImage *image = [self captureview]; Uiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo:), nil); }}
Done, finish the call ...
touchesended Touch-sensitive type