我如何动画一个UIImageView留下UIPageControl向右? [英] How do I animate a UIImageView left to right with UIPageControl?

查看:91
本文介绍了我如何动画一个UIImageView留下UIPageControl向右?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动画一个UIImageView左至右UIPageControl。我有这个code,但它似乎有点奇怪......特别是因为当我到达图像3该怎么办?我会回来,那会陷入困境的的PageControl阵列中的我有它设置方式的意见数学计算:

I want to animate a UIImageView left to right with UIPageControl. I have this code but it seems a bit odd...Specifically because what would I do when I reach image 3? I would have to return and that would mess up the mathematical calculations of the views in the pageControl array the way I have it set up:

    -(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。

When I reach the end, I try to return, the last position is 2 and oldPosition at that time is 1. Which is correct, array goes from 0 to 1 to 2 for a total of 3 positions. But when I return, the log displays position as 1 instead of 2, which is fine, because I reversed...but old position shows 0 instead of 2.

推荐答案

我不能得到它来确定方向,所以我结束了使用手势识别与刷卡左,向右滑动实例。这里是code:

I couldnt get it to determine direction so I ended up using gesture recognizers with swipe left and swipe right instances. Here is the code:

//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];

这篇关于我如何动画一个UIImageView留下UIPageControl向右?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆