Gesture and touch, gesture touch
UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (100,100, 75, 75)];
ImgView. image = [UIImage imageNamed: @ "a"];
ImgView. tag= 200;
[Self. view addSubview: imgView];
// Set to allow touch and gesture execution
ImgView. userInteractionEnabled = YES;
// Create a click gesture
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapGestureClick :)];
// Add the created click gesture to the image object
[ImgView addGestureRecognizer: tap];
// Create a drag gesture
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panGestureClick :)];
[ImgView addGestureRecognizer: pan];
// Create a long-pressed gesture
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPressGestureClick :)];
[ImgView addGestureRecognizer: longPress];
// Create a rotation gesture
UIRotationGestureRecognizer * rotate = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (rotateGestureClick :)];
[ImgView addGestureRecognizer: rotate];
// Create a kneading gesture
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @ selector (pinchGestureClick :)];
// The kneading gesture can be executed simultaneously with other gestures
Pinch. delegate = self;
[ImgView addGestureRecognizer: pinch];
// Create a sweep and vertical sweep gesture
UISwipeGestureRecognizer * hs = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (hsClick :)];
Hs. direction = uiswipegesturerecognizerdireleft | UISwipeGestureRecognizerDirectionRight;
[Self. view addGestureRecognizer: hs];
UISwipeGestureRecognizer * vs = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (vsClick :)];
Vs. direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
[Self. view addGestureRecognizer: vs];
}
-(Void) vsClick :( UISwipeGestureRecognizer *)
{
NSLog (@ "longitudinal scan ");
}
-(Void) hsClick :( UISwipeGestureRecognizer *) hs
{
NSLog (@ "sweep ");
}
// This method is called when the kneading gesture is executed.
-(Void) pinchGestureClick :( UIPinchGestureRecognizer *) pinch
{
Pinch. view. transform = CGAffineTransformScale (pinch. view. transform, pinch. scale, pinch. scale );
[Pinch setScale: 1];
}
// Call this method when performing a rotation gesture
-(Void) rotateGestureClick :( UIRotationGestureRecognizer *) rotate
{
// Roate. rotation the rotation radians of the finger
Rotate. view. transform = CGAffineTransformRotate (rotate. view. transform, rotate. rotation );
// Keep the image and finger in the same radian
[Rotate setRotation: 0];
}
// This method is called by the Chief Executive on time
// Parameter name. view is the object of the added gesture
-(Void) longPressGestureClick :( UILongPressGestureRecognizer *) loginPress
{
LoginPress. view. center = self. view. center;
}
// This method is executed when the drag gesture is executed.
-(Void) panGestureClick :( UIPanGestureRecognizer *) pan
{
// Pan. view is the clicked object
Pan. view. center = [pan locationInView: self. view];
}
// The parameter is the gesture type of the image.
-(Void) tapGestureClick :( UITapGestureRecognizer *) tap
{
NSLog (@ "tap ");
}
-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Return YES, allows the object that adds a gesture to execute multiple gestures at the same time
// Complies with the UIGestureRecognizerDelegate Protocol
-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer author :( UIGestureRecognizer *) otherGestureRecognizer
{
Return YES;
}
(Touch)
// Add an image to the page
UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (100,100, 75, 75)];
ImgView. image = [UIImage imageNamed: @ "a"];
[Self. view addSubview: imgView];
ImgView. tag= 100;
// Allows the current screen to support multi-touch
Self. view. multipleTouchEnabled = YES;
// Allows image objects to support gestures and touch
ImgView. userInteractionEnabled = YES;
// Set the button on the right of the navigation bar
UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc] initWithTitle: @ "Next" style: UIBarButtonItemStylePlain target: self action: @ selector (rightBtnClick :)];
Self. navigationItem. rightBarButtonItem = rightBtn;
}
-(Void) rightBtnClick :( UIBarButtonItem *) bt
{
_ SecondController = [[SecondViewController alloc] init];
_ SecondController. navigationItem. title = @ "Gesture ";
[Self. navigationController pushViewController: _ secondController animated: YES];
}
-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Call this method when you touch the screen
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
{
NSLog (@ "touhesBegan ");
// Obtain the number of fingers on the screen
NSLog (@ "% I", [touches count]);
}
// Call this method when your fingers slide on the screen
-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event
{
UIImageView * imgView = (UIImageView *) [self. view viewwithtagag: 100];
For (UITouch * t in touches ){
// T. view: Obtain the empty object clicked by the finger.
If (t. view = imgView ){
// The image is moved based on the position of the finger on the image.
ImgView. center = [t locationInView: self. view];
}
}
NSLog (@ "moving ");
}
-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event
{
NSLog (@ "end ");
}