私はこのサイトで見つけることができるほとんどすべての投稿を読みました。私はいくつかの非常に有用な投稿を見つけましたが、私の問題を解決する方法を完全には見つけていません。スプリングボード風の画像ビューアを作成しようとしています。ユーザーは次の画像を所定の位置にスライドできますが、途中で停止して戻ってください。PanGestureRecognizerが進むべき道であるという多くの投稿を読みましたが、それで非常に困難な時間を過ごしてきました。
ストーリーボードで、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");
}
}
パンに次の画像が表示されますが、読み込まれる新しいビューにPanGestureRecongizerがありません。ストーリーボードにPANGestureを追加すると、ViewControllerのすべてのビューで使用できるようになると思いました。
これは別の質問かもしれませんが、PanGestureでアニメーションを機能させるにはどうすればよいですか?私は、パンが実際にスライドを行うという印象を誤って(明らかに)受けていました。
よろしくお願いします!!