Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Animation 如何使用UIPageControl从左到右设置UIImageView的动画?_Animation_Uiimageview - Fatal编程技术网

Animation 如何使用UIPageControl从左到右设置UIImageView的动画?

Animation 如何使用UIPageControl从左到右设置UIImageView的动画?,animation,uiimageview,Animation,Uiimageview,我想使用UIPageControl从左到右设置UIImageView的动画。我有这个代码,但它似乎有点奇怪…特别是因为当我到达图3时我会怎么做?我将不得不返回,这将扰乱pageControl数组中视图的数学计算,就像我设置的那样: -(void)createUIViews{ UIImage *image1 = [UIImage imageNamed:@"GiftCard.png"]; firstView = [[UIImageView alloc] ini

我想使用UIPageControl从左到右设置UIImageView的动画。我有这个代码,但它似乎有点奇怪…特别是因为当我到达图3时我会怎么做?我将不得不返回,这将扰乱pageControl数组中视图的数学计算,就像我设置的那样:

    -(void)createUIViews{
        UIImage *image1 = [UIImage imageNamed:@"GiftCard.png"];
        firstView = [[UIImageView alloc] initWithImage:image1];
        firstView.backgroundColor = [UIColor grayColor];
        firstView.tag = 1;
        firstView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
        [self.view addSubview:firstView];
        CGRect newFrame = firstView.frame;
        newFrame.origin.x += 40;    // shift right by 50pts
        newFrame.origin.y += 40;

        [UIView animateWithDuration:1.0
                         animations:^{
                             firstView.frame = newFrame;
                         }];

        UIImage *image2 = [UIImage imageNamed:@"MyCard.png"];
        secondView = [[UIImageView alloc] initWithImage:image2];
        secondView.backgroundColor = [UIColor grayColor];
        secondView.tag = 1;
        secondView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);

        UIImage *image3 = [UIImage imageNamed:@"GiftCard.png"];
        thirdView = [[UIImageView alloc] initWithImage:image3];
        thirdView.backgroundColor = [UIColor grayColor];
        thirdView.tag = 1;
        thirdView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);


    }

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createUIViews];
    // Set up the views array with 3 UIImageViews
    views = [[NSArray alloc] initWithObjects:firstView, secondView, thirdView, nil];
    // Set pageControl's numberOfPages to 3
    self.pageControl.numberOfPages = [views count];
    // Set pageControl's currentPage to 0
    self.pageControl.currentPage = 0;

    // Call changePage each time value of pageControl changes
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
}

- (IBAction) changePage:(id)sender {
    NSLog(@"pageControl position %d", [self.pageControl currentPage]);
    int oldPageControlPosition = [self.pageControl currentPage] - 1;
    NSLog(@"old pageControl position %d", oldPageControlPosition);

    //0 Get the currentview as referenced by pageControl
    UIImageView *oldView = [views objectAtIndex:oldPageControlPosition];
    //NSLog(@"views objectAtIndex = %@",[views objectAtIndex:[self.pageControl currentPage]]);

    //1 Animate out the old view
    CGRect oldViewFrame = oldView.frame;
    oldViewFrame.origin.x -= 300;

    [UIView animateWithDuration:2.0
                          delay: 0.5
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         oldView.frame = oldViewFrame;
                     }

                     completion:nil];


    //3 Get next view from array
    UIImageView *nextView = [views objectAtIndex:[self.pageControl currentPage]];

    //4 Add it to mainview
    [self.view addSubview:nextView];

    //5 Animate in
    CGRect nextViewFrame = nextView.frame;
    nextViewFrame.origin.x += 40;    // shift right by 50pts
    nextViewFrame.origin.y += 40;

    [UIView animateWithDuration:1.0
                     animations:^{
                         nextView.frame = nextViewFrame;
                     }];
}

当我到达终点时,我尝试返回,最后一个位置是2,当时的oldPosition是1。这是正确的,数组从0到1到2,总共有3个位置。但当我返回时,日志显示的位置是1而不是2,这很好,因为我反转了…但旧位置显示的是0而不是2。

我无法让它确定方向,所以我最终使用手势识别器来识别左扫和右扫实例。代码如下:

//Help determine direction of swipe
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeNext:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.scrollView addGestureRecognizer:swipeLeft];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipePrev:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.scrollView addGestureRecognizer:swipeRight];