Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Add a swipe gesture recognizer. When the user swipes determine which direction and handle the image swap.</p> <p>[EDIT] - Imagine a square divided into four equal sections. The top left section has an index of zero, the top right an index of one, the bottom left an index of 2 and finally the bottom right an index of 3. The code below checks the current index and from that it determines if an image swap can be made, if not it does nothing.</p> <p>This code is from the top of my head so there may be syntax errors but the logic is sound (I hope :D ).</p> <pre><code>- (void) viewDidLoad { // turn on user interaction on the image view as its off by default. [self.imageView setUserInteractionEnabled:TRUE]; UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)]; [self.imageView addGestureRecognizer:recognizer]; self.currentImageIndex = 0; self.images = [NSArray arrayWithObjects:[UIImage imageNamed:@"top-left"],[UIImage imageNamed:@"top-right"],[UIImage imageNamed:@"bottom-left"],[UIImage imageNamed:@"top-right"],nil]; } -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) { if (self.currentImageIndex == 0 || self.currentImageIndex == 2) self.currentImageIndex++; // change to image to the right else return; // do nothing } else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { if (self.currentImageIndex == 1 || self.currentImageIndex == 3) self.currentImageIndex--; // change to the image to the left else return; // do nothing } else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp) { if (self.currentImageIndex == 2 || self.currentImageIndex == 3) self.currentImageIndex -= 2; // change to the above image else return; // do nothing } else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown) { if (self.currentImageIndex == 0 || self.currentImageIndex == 1) self.currentImageIndex += 2; // change to the above image else return; // do nothing } [UIView animationWithDuration:0.5 animations:^{ [self.imageView setAlpha:0]; } completion^(BOOL finished){ if (finished) { [UIView animationWithDuration:0.5 animations:^{ [self.imageView setImage[self.images objectAtIndex:self.currentImageIndex]]; [self.imageView setAlpha:1]; }]; } }]; } </code></pre>
    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. 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