[英]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. 我已经读过很多文章,PanGestureRecognizer是必经之路,但是一直很困难。
In Storyboard, I created my ViewController and added a PanGestureRecognizer to the ViewController and added an action. 在情节提要中,我创建了ViewController,并向ViewController添加了PanGestureRecognizer并添加了一个动作。
Here is my .h: 这是我的.h:
@interface PanViewController : UIViewController
- (IBAction)PanGesture:(UIPanGestureRecognizer *)recognizer;
@end
Here is my .m: 这是我的.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. 它显示了平底锅上的下一个图像,但是在新视图中没有加载PanGestureRecongizer。我以为,当我在Storyboard中添加PANGesture时,它将对ViewController中的所有视图可用。
This may be a separate question, but how do I get the animation to work on the PanGesture? 这可能是一个单独的问题,但是如何使动画在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. 所以,像汤姆·欧文说,我用分页一个UIScrollView。 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. 每次都确实创建了一个新的手势识别器并添加了页面调度,但这确实很复杂,而且我无法使其正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.