繁体   English   中英

将UIPanGestureRecognizer添加到新创建的UIView中,并用幻灯片替换当前视图

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

我已经阅读了几乎所有可以在该站点上找到的帖子。 我发现了一些非常有用的帖子,但是还没有找到如何解决我的问题的方法。 我正在尝试创建一个弹跳式图像查看器,用户可以将下一张图像滑动到位,但要停一半然后返回。 我已经读过很多文章,PanGestureRecognizer是必经之路,但是一直很困难。

在情节提要中,我创建了ViewController,并向ViewController添加了PanGestureRecognizer并添加了一个动作。

这是我的.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");
    }
}

它显示了平底锅上的下一个图像,但是在新视图中没有加载PanGestureRecongizer。我以为,当我在Storyboard中添加PANGesture时,它将对ViewController中的所有视图可用。

这可能是一个单独的问题,但是如何使动画在PanGesture上运行? 我不正确地(显然)给人一种印象,那就是潘可以有效地执行幻灯片。

谢谢大家的帮助!!

所以,像汤姆·欧文说,我用分页一个UIScrollView。 每次都确实创建了一个新的手势识别器并添加了页面调度,但这确实很复杂,而且我无法使其正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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