UIGestureRecognizer(제스처),터치(터치)

24007 단어 gesture
  1 // touch(  )、   

  2 //         delegate         view   addGestureRecognizer         

  3 #import "ViewController.h"

  4 @interface ViewController ()<UIGestureRecognizerDelegate> //         

  5 {

  6     /**   UIImageView      image */

  7     UIImageView* image;

  8 }

  9 

 10 // touch      (3.2           ) touch         

 11 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; //   

 12 //{

 13 //    UITouch* touch = [touches anyObject]; //     

 14 //    CGPoint poin = [touch locationInView:self]; //       

 15 //}

 16 -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;  //

 17 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;  //   

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

 19 @end

 20 @implementation ViewController

 21 - (void)viewDidLoad {

 22     [super viewDidLoad];

 23     

 24     image  =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

 25     //        

 26     image.image = [UIImage imageNamed:@"  07-6"];

 27     //          

 28     image.contentMode = UIViewContentModeScaleAspectFit;

 29     //      

 30     [self.view addSubview:image];

 31     //        

 32     image.userInteractionEnabled = YES;

 33     

 34     //       UIGestureRecognizer

 35     /*

 36     UITapGestureRecognizer, //   

 37     UIPinchGestureRecognizer, //    

 38     UIRotationGestureRecognizer, //   

 39     UISwipeGestureRecognizer,  //   ,    

 40     UIPanGestureRecognizer,  //   ,    

 41     UILongPressGestureRecognizer  //   

 42      

 43              ,  :Tap LongPress、Swipe Pan、  Tap   Tap  

 44      

 45                   ,       ,          ,          ;

 46      

 47             :       ,      ,           。           ,             ,           ,                 。

 48      [A requireGestureRecognizerToFail: B];  ,      A     ,  A       ,       ,        B         。

 49      */

 50     //   (  )

 51     UITapGestureRecognizer* oneTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(oneTap:)];

 52     //   ,numberOfTapsRequired = 2     //         

 53     oneTap.numberOfTapsRequired = 1; //       

 54     //           1

 55     oneTap.numberOfTouchesRequired = 1;

 56     //    0

 57     int a= oneTap.numberOfTouches;

 58     NSLog(@"a = %d",a);

 59     //       

 60     [image addGestureRecognizer:oneTap];

 61     

 62     //   

 63     UITapGestureRecognizer* twoTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twoTap:)];

 64     twoTap.numberOfTapsRequired = 2;

 65     [image addGestureRecognizer:twoTap];

 66 

 67     //(  )、                       (       )

 68     [oneTap requireGestureRecognizerToFail:twoTap]; //       

 69     

 70     

 71     //   

 72     UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];

 73     [image addGestureRecognizer:pinch];

 74     

 75     //   

 76     UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];

 77     [image addGestureRecognizer:rotation];

 78     

 79     //

 80     UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

 81     //             

 82     swipe.direction = UISwipeGestureRecognizerDirectionRight;

 83     typedef  enum{

 84         UISwipeGestureRecognizerDirectionDown, //

 85         UISwipeGestureRecognizerDirectionLeft, //   

 86         UISwipeGestureRecognizerDirectionRight, //   

 87         UISwipeGestureRecognizerDirectionUp, //

 88     }UISwipeGestureRecognizerDirection;

 89     

 90     [image addGestureRecognizer:swipe];

 91     

 92     //

 93     UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

 94     //    -1;      

 95     pan.maximumNumberOfTouches = 1; //

 96     //    1;      

 97     pan.minimumNumberOfTouches = 2; //        Pan 

 98     

 99     [image addGestureRecognizer:pan];

100     

101     //   

102     UILongPressGestureRecognizer* zer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lon:)];

103     //       

104     zer.minimumPressDuration = 0.5; // 2 

105     [image addGestureRecognizer:zer];

106 }

107 //   (  )

108 - (void)oneTap:(UITapGestureRecognizer*)tap{

109 //    if (tap.numberOfTapsRequired == 1) {

110 //        NSLog(@"  ");

111 //    }else if(tap.numberOfTapsRequired == 2){

112 //        NSLog(@"  ");

113 //    }

114     

115         NSLog(@"  ");

116 }

117 //   

118 - (void)twoTap:(UITapGestureRecognizer*)two{

119     NSLog(@"  ");

120 }

121 

122 //   

123 - (void)pinch:(UIPinchGestureRecognizer*)pinch{

124     //      (    )

125     pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

126     //         1,  1  ,  1  

127     pinch.scale = 1;

128     //       

129     // pinch.view.transform = CGAffineTransformIdentity;

130     

131     //       (                     )

132     pinch.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, pinch.scale, pinch.scale);

133 }

134 //   

135 - (void)rotation:(UIRotationGestureRecognizer*)rotation{

136     //   (    ) //      

137     rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);

138     //

139     rotation.rotation = 0;

140     

141     // rotation.view.transform = CGAffineTransformRotate(CGAffineTransformIdentity, rotation.rotation);

142 }

143 //

144 - (void)swipe:(UISwipeGestureRecognizer*)swipe{

145     NSLog(@"    ");

146 }

147 //

148 - (void)pan:(UIPanGestureRecognizer*)pan{

149     //   (    ) //      

150     CGPoint translation = [pan translationInView:self.view];

151     pan.view.center = CGPointMake(pan.view.center.x + translation.x, pan.view.center.y + translation.y);

152     [pan setTranslation:CGPointZero inView:self.view];

153 }

154 //   

155 -(void)lon:(UILongPressGestureRecognizer*)longe{

156     NSLog(@"  ");

157 }

158 

159 //     

160 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

161     return YES;

162 }

163 -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

164     return YES;

165 }

166 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

167     return YES;

168 }

169 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

170     return YES;

171 }

172 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

173     return YES;

174 }

좋은 웹페이지 즐겨찾기