Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, a UIGestureRecognizer can be added to a UIImageView. As stated in the other answer, it is very important to remember to enable user interaction on the image view by setting its <code>userInteractionEnabled</code> property to <code>YES</code>. UIImageView inherits from UIView, whose user interaction property is set to <code>YES</code> by default, however, UIImageView's user interaction property is set to <code>NO</code> by default.</p> <p>From the <a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIImageView_Class/Reference/Reference.html">UIImageView docs:</a></p> <blockquote> <p>New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.</p> </blockquote> <p>Anyway, on the the bulk of the answer. Here's an example of how to create a <code>UIImageView</code> with a <code>UIPinchGestureRecognizer</code>, a <code>UIRotationGestureRecognizer</code>, and a <code>UIPanGestureRecognizer</code>.</p> <p>First, in <code>viewDidLoad</code>, or another method of your choice, create an image view, give it an image, a frame, and enable its user interaction. Then create the three gestures as follows. Be sure to utilize their delegate property (most likely set to self). This will be required to use multiple gestures at the same time.</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; // set up the image view UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage"]]; [imageView setBounds:CGRectMake(0.0, 0.0, 120.0, 120.0)]; [imageView setCenter:self.view.center]; [imageView setUserInteractionEnabled:YES]; // &lt;--- This is very important // create and configure the pinch gesture UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)]; [pinchGestureRecognizer setDelegate:self]; [imageView addGestureRecognizer:pinchGestureRecognizer]; // create and configure the rotation gesture UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureDetected:)]; [rotationGestureRecognizer setDelegate:self]; [imageView addGestureRecognizer:rotationGestureRecognizer]; // creat and configure the pan gesture UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)]; [panGestureRecognizer setDelegate:self]; [imageView addGestureRecognizer:panGestureRecognizer]; [self.view addSubview:imageView]; // add the image view as a subview of the view controllers view } </code></pre> <p>Here are the three methods that will be called when the gestures on your view are detected. Inside them, we will check the current state of the gesture, and if it is in either the began or changed <code>UIGestureRecognizerState</code> we will read the gesture's scale/rotation/translation property, apply that data to an affine transform, apply the affine transform to the image view, and then reset the gestures scale/rotation/translation. </p> <pre><code>- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGFloat scale = [recognizer scale]; [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)]; [recognizer setScale:1.0]; } } - (void)rotationGestureDetected:(UIRotationGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGFloat rotation = [recognizer rotation]; [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)]; [recognizer setRotation:0]; } } - (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGPoint translation = [recognizer translationInView:recognizer.view]; [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)]; [recognizer setTranslation:CGPointZero inView:recognizer.view]; } } </code></pre> <p>Finally and very importantly, you'll need to utilize the <a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html">UIGestureRecognizerDelegate</a> method <code>gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer</code> to allow the gestures to work at the same time. If these three gestures are the only three gestures that have this class assigned as their delegate, then you can simply return <code>YES</code> as shown below. However, if you have additional gestures that have this class assigned as their delegate, you may need to add logic to this method to determine which gesture is which before allowing them to all work together.</p> <pre><code>- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } </code></pre> <p>Don't forget to make sure that your class conforms to the <a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html">UIGestureRecognizerDelegate</a> protocol. To do so, make sure that your interface looks something like this:</p> <pre><code>@interface MyClass : MySuperClass &lt;UIGestureRecognizerDelegate&gt; </code></pre> <p>If you prefer to play with the code in a working sample project yourself, the sample project I've created containing this code <a href="https://github.com/NSPostWhenIdle/GestureImageView">can be found here.</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload