Article as long as you have a little foundation should be able to read, the article only for learning communication
#import"ViewController.h"@interfaceViewcontroller ()@property (Nonatomic,RetainUiimageview *imageview;@property (Nonatomic,AssignNsinteger index;Subscript@property (Nonatomic,RetainNsmutablearray *images;Image name Array@end@implementationViewcontroller#加载视图-(void) viewdidload{[super Viewdidload]; Layout imageview[selflayoutimageview]; 1. Create a pat gesture [selftapgesturerecognizer]; 2. Create a sweep gesture [selfswipegesturerecognizer]; 3. Create the Chang ' an gesture [selflongpressgesturerecognizer]; 4. Create a panning gesture [selfpangesturetecognizer]; 5. Create a pinch gesture [selfpinchgesturerecognizer]; 6. Create a rotation gesture [selfrotationgesturerecognizer]//7. Creating edge gestures [selfscreenedgepangesturerecognizer];
}
# #布局ImageView-(void) layoutimageview{Creating objectsUiimageview *imageview =[[Uiimageview Alloc]initwithframe:CGRectMake (0,0,375,667)];Configuration Properties Imageview.backgroundcolor =[Uicolor Purplecolor];Set Picture Imageview.image =[UIImage imagenamed:@ "Car1.jpg"];Add Parent View [Self.view Addsubview:imageview];Assigns a picture view object created to a propertySelf.imageview =imageview;Open User interactionself.imageView.userInteractionEnabled =YES;Self.images =[Nsmutablearray array];for (int i =1; i<9; i++) {NSString * ImageName =[NSString stringWithFormat:@ "Car%d.jpg", I]; [_images addobject:imagename];}_index = 1;}#pragma fear gesturesCreate a tap gesture-(void) tapgesturerecognizer{Create a Gesture ObjectUITapGestureRecognizer *tap =[[UITapGestureRecognizer Alloc]initwithtarget:Self action:@selector (tapaction:)];Configuration PropertiesNumber of taps tap.numberoftapsrequired =1;Tap the number of fingers tap.numberoftouchesrequired =1;Speak gestures added to the specified view [_imageview Addgesturerecognizer:tap];}Tap Event-(void) Tapaction: (UITapGestureRecognizer *) tap{Picture ToggleNSLog (@ "Clap"); _index + +;if (_index = =8) {_index =1;}Self.imageView.image =[UIImage Imagenamed:_images[_index]];}#pragma sweep gestureSweep Gesture-(void) swipegesturerecognizer{Uiswipegesturerecognizer *swipe =[[Uiswipegesturerecognizer Alloc]initwithtarget:Self action:@selector (swipeaction:)];Configuration PropertiesA sweep gesture can only have two directions (up and down) or (left or right)If you want to support up and down sweep then a gesture cannot be implemented need to create two gestures swipe.direction =Uiswipegesturerecognizerdirectionleft;Add to specified view [_imageview addgesturerecognizer:swipe];Uiswipegesturerecognizer *swipe2 =[[Uiswipegesturerecognizer Alloc]initwithtarget:Self action:@selector (swipeaction:)];swipe2.direction =Uiswipegesturerecognizerdirectionright; [_imageview addgesturerecognizer:swipe2];}Sweep Event-(void) Swipeaction: (Uiswipegesturerecognizer *) swipe{NSLog (@ "Sweep");if (swipe.direction = =Uiswipegesturerecognizerdirectionright) {NSLog (@ "right sweep"); _index--;if (_index <1) {_index =7; } _imageview.image =[UIImage Imagenamed:_images[_index]];}Elseif (swipe.direction = =Uiswipegesturerecognizerdirectionleft) {NSLog (@ "Sweep left"); _index + +;if (_index = =8) {_index=1; } _imageview.image =[UIImage Imagenamed:_images[_index]];}}#pragma long press gestureCreate a long press gesture-(void) longpressgesturerecognizer{Uilongpressgesturerecognizer *longpress =[[Uilongpressgesturerecognizer Alloc]initwithtarget:Self action:@selector (longpressaction:)];Shortest long press time longpress.minimumpressduration =2;Allowed to move maximum distance longpress.allowablemovement =1;Add to specified view [_imageview addgesturerecognizer:longpress];}Long-press Event-(void) Longpressaction: (Uilongpressgesturerecognizer *) longpress{NSLog (@ "long press");For Changan There is a start and end stateif (longpress.state = =Uigesturerecognizerstatebegan) {NSLog (@ "Long Press to start");Save a picture to an albumUIImage *image =[UIImage Imagenamed:_images[_index]];Uiimagewritetosavedphotosalbum (Image,NilNilnil);}Elseif (longpress.state = =uigesturerecognizerstateended) {NSLog (@ "Long press End");}}#pragma panning gesturesCreate a panning gesture-(void) pangesturetecognizer{Uipangesturerecognizer *pan =[[Uipangesturerecognizer Alloc]initwithtarget:Self action:@selector (panaction:)];Add to specified view [_imageview Addgesturerecognizer:pan];}Create a Translation event-(void) Panaction: (Uipangesturerecognizer *) pan{Get the position of the gestureCgpoint position =[pan Translationinview:_imageview];Pan Exchange via Stransform _imageview.transform =Cgaffinetransformtranslate (_imageview.transform, position.x, POSITION.Y);Set the increment to zero [pan settranslation:Cgpointzero Inview:_imageview];}#pragma pinch gesture-(void) pinchgesturerecognizer{Uipinchgesturerecognizer *pinch =[[Uipinchgesturerecognizer Alloc]initwithtarget:Self action:@selector (pinchaction:)];Add to specified view [_imageview addgesturerecognizer:pinch];}Add pinch Event-(void) Pinchaction: (Uipinchgesturerecognizer *) pinch{Kneading _imageview.transform of view by transform (change)Cgaffinetransformscale (_imageview.transform, Pinch.scale, Pinch.scale);Set the scale to 1pinch.scale =1;}#pragma rotation gesturesCreate a rotation gesture-(void) rotationgesturerecognizer{Uirotationgesturerecognizer *rotation =[[Uirotationgesturerecognizer Alloc]initwithtarget:Self action:@selector (rotationaction:)];Add to the specified view [_imageview addgesturerecognizer:rotation];}Rotation Event-(void) Rotationaction: (Uirotationgesturerecognizer *) rote{Rotation transformation _imageview.transform by transform =Cgaffinetransformrotate (_imageview.transform, rote.rotation);Position the rotation angle to 0rote.rotation =0;}#pragma edge gestures//Create edge gesture-(void) screenedgepangesturerecognizer{uiscreenedgepangesturerecognizer *screenpan = [[ Uiscreenedgepangesturerecognizer Alloc]initwithtarget: SelfAction:@selector (screenpanaction:)];[ _imageview Addgesturerecognizer:screenpan];} //Create Edge event-(void) Screenpanaction: (uiscreenedgepangesturerecognizer *) screenpan{NSLog (@ "Edge");} -(void) didreceivememorywarning {[super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Seven gestures that iOS learns must know