Question

I am working in C4 and want to be able to press down on a shape and drag it at a slow speed upward. I have been working with the "Getting the UITouch objects for a UIGestureRecognizer" tutorial and other TouchesMoved and TouchesBegan Objective-C tutorials but the gesture is not applying to the shape. The project builds but the shape still does not move when you press and drag it. Is there a particular way C4 applies gestures to shapes?

This is my code:

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
Was it helpful?

Solution

C4 has a method that simplifies the process of adding gestures to any visible object (e.g. C4Shape, C4Movie, C4Label...).

NOTE: you will need to create a subclass of C4Shape in which you can create some custom methods.

For instance, the following will add a tap gesture to an C4Shape:

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

The addGesture:name:action: method is listed in the C4Control documentation, and is defined like:

Adds a gesture to an object.

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

The (C4GestureType)type can be any one of:

  • TAP
  • SWIPERIGHT
  • SWIPELEFT
  • SWIPEUP
  • SWIPEDOWN
  • PAN
  • LONGPRESS

The following gestures are available, but haven't yet been tested:

  • PINCH
  • ROTATION

The (NSString *)gestureName allows you to specify a unique name for the gesture.

The (NSString *)methodName allows you to specify the method that the gesture will trigger.

So... getting back to the example above:

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

...will add to the s object a TAP gesture called singleTapGesture which, when triggered, will run a -(void)tap; method (which needs to have already defined in your class).


You can apply the technique I described above, but instead use:

[s addGesture:PAN name:@"panGesture" action:@"move:];

you will also have to define a

-(void)move:(id)sender;

method in your shape's class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top