ios5 – 如何将UILongPressGestureRecognizer应用于C4中的形状?

我在C4工作,希望能够按下一个形状并以慢速向上拖动它.

我一直在使用“为UIGestureRecognizer获取UITouch对象”教程和其他TouchesMoved和TouchesBegan Objective-C教程,但手势不适用于形状.项目构建但按下并拖动时形状仍然不会移动.有没有一种特殊的方式C4将手势应用于形状?

这是我的代码:

DragGestureRecognizer.h

#import <UIKit/UIKit.h>


@interface DragGestureRecognizer : UILongPressGestureRecognizer {

 }

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

@end

@protocol DragGestureRecognizerDelegate <UIGestureRecognizerDelegate>
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches     andEvent:(UIEvent *)event;
@end

C4WorkSpace.m:

#import "C4WorkSpace.h"
#import "DragGestureRecognizer.h"


@implementation DragGestureRecognizer

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

    [super touchesMoved:touches withEvent:event];

    if ([self.delegate respondsToSelector:@selector(gestureRecognizer:movedWithTouches:andEvent:)]) {
    [(id)self.delegate gestureRecognizer:self movedWithTouches:touches andEvent:event];

    }
}

@end 

@implementation C4WorkSpace {
    C4Shape *circle;
}

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
        NSLog(@"Long Press");
    }
}

-(void)setup {

    circle = [C4Shape ellipse:CGRectMake(5,412,75,75)];
    [self.canvas addShape:circle];
}


 @end

最佳答案 C4具有简化向任何可见对象添加手势的过程的方法(例如,C4Shape,C4Movie,C4Label ……).

注意:您需要创建C4Shape的子类,您可以在其中创建一些自定义方法.

例如,以下内容将向C4Shape添加点击手势:

C4Shape *s = [C4Shape rect:CGRectMake(..)];
[s addGesture:TAP name:@"singleTapGesture" action:@"tap"];

addGesture:name:action:方法列在C4Control文档中,定义如下:

向对象添加手势.

- (void)addGesture:(C4GestureType)type name:(NSString *)gestureName action:(NSString *)methodName

(C4GestureType)类型可以是以下任何一种:

> TAP
> SWIPERIGHT
> SWIPELEFT
> SWIPEUP
> SWIPEDOWN
>潘
> LONGPRESS

以下手势可用,但尚未经过测试:

> PINCH
>旋转

(NSString *)gestureName允许您为手势指定唯一名称.

(NSString *)methodName允许您指定手势将触​​发的方法.

所以……回到上面的例子:

[s addGesture:TAP name:@"singleTapGesture" action:@"tap"];

…将为s对象添加一个名为singleTapGesture的TAP手势,当触发时,它将运行 – (void)tap;方法(需要在您的类中定义).

您可以应用我上面描述的技术,但改为使用:

[s addGesture:PAN名称:@“panGesture”动作:@“move:];

你还必须定义一个

– (无效)移动:(ID)发送者;

在你的形状类中的方法.

点赞