我已经阅读了几乎可以在该网站上找到的所有帖子。我发现了一些非常有用的帖子,但还没有找到解决我的问题的方法。我正在尝试创建一个跳板式图像查看器,用户可以将下一个图像滑入到位,但中途停止并返回。我在许多帖子中读到 PanGestureRecognizer 是可行的方法,但一直很难使用它。
在 Storyboard 中,我创建了 ViewController 并将 PanGestureRecognizer 添加到 ViewController 并添加了一个动作。
这是我的.h:
@interface PanViewController : UIViewController
- (IBAction)PanGesture:(UIPanGestureRecognizer *)recognizer;
@end
这是我的.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");
}
}
它显示了 pan 上的下一个图像,但是在加载的新 View 上没有 PanGestureRecongizer。我认为当我在 Storyboard 中添加 PANGesture 时,它将使其可用于 ViewController 中的所有视图。
这可能是一个单独的问题,但是如何让动画在 PanGesture 上工作?我(显然)错误地认为 Pan 会执行幻灯片。
谢谢大家的帮助!!