How to apply UILongPressGestureRecognizer to a form in C4?

I work on C4 and want to be able to push on the shape and drag it at a slow speed up. I work with the tutorials "Getting UITouch objects for the UIGestureRecognizer tutorial" and other TouchesMoved and TouchesBegan Objective-C tutorials, but this gesture does not apply to the form. The project is being built, but when you click and drag its form does not move. Is there a special way that 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 
+4
source share
1 answer

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

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

For example, the following will add a binding gesture to C4Shape:

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

The addGesture:name:action: method is specified in the C4Control documentation and defined as:

Adds a gesture to the object.

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

(C4GestureType)type can be any of:

  • Tap
  • SWIPERIGHT
  • SWIPELEFT
  • Swipeup
  • SWIPEDOWN
  • Pan
  • LONGPRESS

The following gestures are available but have not yet been tested:

  • Pinch
  • TURN

(NSString *)gestureName allows (NSString *)gestureName to specify a unique name for gestures.

(NSString *)methodName allows you to specify the method that will call this gesture.

So ... back to the above example:

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

... will add a gesture called singleTapGesture to the object s a TAP , which, when launched, will launch the -(void)tap; method -(void)tap; (which should already be defined in your class).


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

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

you will also need to determine

-(void)move:(id)sender;

in your form class.

+2
source

Source: https://habr.com/ru/post/1411624/


All Articles