1

配列には 8UIImageViewつの画像があり、一度に配列から 1 つの画像が表示されます。また、ユーザーが画像UIPanGestureRecognizerUIImageViewで指を横に動かすと、画像が変化します(画像の上部にある目に見えないスクローラーのようなものです)。パン ジェスチャの変換値を使用して画像を変更しています。これはすべて機能しますが、私が望む方法ではありません。画像の変化が速すぎます。短い指のパンで画像をスクロールするだけです。画像を変更するために長いパンを必要とする方法はありますか? または、ユーザーにとってこれを「よりスムーズ」にする他の方法はありますか?

.h

@interface FirstViewController : UIViewController
    @property (nonatomic, retain) IBOutlet UIImageView *imageView;
    - (IBAction)pan:(UIGestureRecognizer *)panGesture;

.m

@interface FirstViewController () {
    NSArray * imageArray;
    int _currentIndex;
}

@end

@implementation FirstViewController

- (IBAction)pan:(UIPanGestureRecognizer *)panGesture {
  //  NSLog(@"panned");
    CGPoint translation = [panGesture translationInView:self.view];
    [self.panLabel setText: NSStringFromCGPoint(translation)];
    NSLog(@"%@ translation", NSStringFromCGPoint(translation));

    if (translation.x<-1) {
        _currentIndex=(_currentIndex<=0)?0:_currentIndex-1;
        UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
        [self.imageView setImage:img];
    }
    else if(translation.x>1){
        _currentIndex=(_currentIndex>=7)?7:_currentIndex+1;
        UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
        [self.imageView setImage:img];
    }
    [panGesture setTranslation:CGPointZero inView:self.view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    imageArray = [[NSArray alloc]
                  initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", @"image6.jpg", @"image7.jpg", @"image8.jpg", nil];
}
4

1 に答える 1

1

コードを変更して、X パンを経時的に追跡することができます。これにより、パン量の変数を設定できます。これを修正したコードは次のとおりです。

@interface FirstViewController () {
    NSArray * imageArray;
    int _currentIndex;

    CGFloat _currentPanXAmount;
    CGFloat _panThreshold;
}

@end

@implementation FirstViewController

- (IBAction)pan:(UIPanGestureRecognizer *)panGesture {
    CGPoint translation = [panGesture translationInView:self.view];

    _currentPanXAmount += translation.x;

    [self.panLabel setText: NSStringFromCGPoint(translation)];
    NSLog(@"%@ translation", NSStringFromCGPoint(translation));

    if (_currentPanXAmount < (-1 * _panThreshold)) {
        _currentIndex=(_currentIndex<=0)?0:_currentIndex-1;
        UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
        [self.imageView setImage:img];
        _currentPanXAmount = 0.0;
    }
    else if(_currentPanXAmount > _panThreshold){
        _currentIndex=(_currentIndex>=7)?7:_currentIndex+1;
        UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
        [self.imageView setImage:img];
        _currentPanXAmount = 0.0;
    }
    [panGesture setTranslation:CGPointZero inView:self.view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    imageArray = [[NSArray alloc]
                  initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", @"image6.jpg", @"image7.jpg", @"image8.jpg", nil];

    // Make this larger for slower transitions and smaller for faster transitions.
    _panThreshold = 10.0;
}
于 2012-12-14T15:57:34.800 に答える