简体   繁体   中英

Adding UIPanGestureRecognizer to newly created UIView, and replace current view with slide

I have read almost every post I can find on this site. I have found some extremely useful posts, but have not quite found how to solve my problem. I am trying to create a springboardish image viewer, were a user can slide the next image into place, but stop halfway though and go back. I have read on numerous posts that a PanGestureRecognizer is the way to go, but have been having a very difficult time with it.

In Storyboard, I created my ViewController and added a PanGestureRecognizer to the ViewController and added an action.

Here is my .h:

@interface PanViewController : UIViewController
- (IBAction)PanGesture:(UIPanGestureRecognizer *)recognizer;
@end

Here is my .m:

@interface PanViewController ()
@property (nonatomic, retain) NSArray *PhotoBundle;
@property (nonatomic, retain) NSMutableArray *PhotoArray;
@property (nonatomic, retain) NSMutableArray *ImgViewArray;
@end

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//set i to the correct range if needed.
if (i > 0 && 1 <= _PhotoArray.count) {

}
else i = 0;

//create photo bundle from directory
_PhotoBundle = [[NSBundle mainBundle] pathsForResourcesOfType:@".jpg"inDirectory:@"Otter_Images"];
//create array from bundle of paths.
_PhotoArray = [[NSMutableArray alloc] initWithCapacity:_PhotoBundle.count];
for (NSString* path in _PhotoBundle)
{
    [_PhotoArray addObject:[UIImage imageWithContentsOfFile:path]];
}

//create initial image view
UIImage *currentImage = [[UIImage alloc] init];
currentImage = [_PhotoArray objectAtIndex:i];
UIImageView *currentIV = [[UIImageView alloc]initWithFrame:CGRectMake(100,100, 100, 100)];
[currentIV setImage:currentImage];
[self.view addSubview:currentIV];
//increment i to progress through array.
i++;
}

- (IBAction)PanGesture:(UIPanGestureRecognizer *)recognizer {
    [self GestureDirection:recognizer];
}

- (void)GestureDirection:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint direction = [gestureRecognizer velocityInView:self.view];

if(direction.x > 0)
{
    NSLog(@"gesture went right");
    UIImage *nextImg = [[UIImage alloc] init];
    nextImg = [_PhotoArray objectAtIndex:i];
    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(100,100, 100, 100)];
    UIImageView *nextIV = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 100, 100)];
    [nextIV setImage:nextImg];
    [newView addSubview:nextIV];

    self.view = newView;
}
else
    {
        NSLog(@"gesture went left");
    }
}

It shows the next image on the pan, but then there is no PanGestureRecongizer on the new View that is loaded in. I thought that when I added the PANGesture in Storyboard, it would make it available to all views in the ViewController.

This may be a separate question, but how do I get the animation to work on the PanGesture? I was incorrectly (obviously) under the impression that a Pan would do the slide in effect.

Thank you all for your help!!

So like Tom Irving said, I used a UIScrollView with paging. I did play around with creating a new gesture recognizer each time and adding paging, but it was really complicated and I was never able to get it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM