动画和ScrollView

时间:2014-05-22 09:53:34

标签: ios objective-c uiimageview

我目前正在尝试构建一个每30秒移动到下一张照片的图像查看器,这也允许手动向左/向右滑动到下一张/上一张照片。

到目前为止,我已经构建了两个独立的应用程序,一个具有滑动功能,另一个具有计时器。我在网上尝试了很多教程,我正在寻找有人指出我正确的方向。


此代码每30秒切换一次图像

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIImageView *animatedImageView = [[UIImageView alloc] 
    initWithFrame:CGRectMake(0, 55,       400,     550)];
    [self.view addSubview:animatedImageView];

     animatedImageView.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"IMG_0052.JPG"],
                                     [UIImage imageNamed:@"IMG_0054.JPG"],
                                     [UIImage imageNamed:@"IMG_0081.JPG"],
                                     nil];
    animatedImageView.animationDuration = 30.0 * [animatedImageView.animationImages count];
    [animatedImageView startAnimating];
    [self.view addSubview: animatedImageView];

 }

这第二段代码具有滑动功能,我希望将两者结合起来

- (void)viewDidLoad
{
    [super viewDidLoad];


    UIImage *image1 = [UIImage imageNamed:@"workExample.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"IMG_0054.JPG"];
    UIImage *image3 = [UIImage imageNamed:@"IMG_0052.JPG"];


    imageArray = [NSArray arrayWithObjects:image1, image2, image3, nil];

    for (int i = 0; i < [imageArray count]; i++) {
        //This is to create a imageview for every single pagecontrol
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        imageView.image = [imageArray objectAtIndex:i];
        [self.scrollView addSubview:imageView];
    }

        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imageArray count],     scrollView.frame.size.height);
}

1 个答案:

答案 0 :(得分:0)

我没有使用scrollview并以不同的方式实现。我希望它符合您的需求

在.h

@interface ViewController : UIViewController
    {
       NSMutableArray *imagesToAnimate;
       UIImageView *animatedImageView;
    }

    @property (strong, nonatomic) IBOutlet UIView *imageGalleryView;

viewDidLoad

animatedImageView = [[UIImageView alloc] initWithFrame:self.imageGalleryView.bounds];
imagesToAnimate = [NSMutableArray arrayWithArray:[NSArray arrayWithObjects:
                                                  [UIImage imageNamed:@"IMG_0052.JPG"],
                                                  [UIImage imageNamed:@"IMG_0054.JPG"],
                                                  [UIImage imageNamed:@"IMG_0081.JPG"],
                                                  nil]];
[self.imageGalleryView addSubview:animatedImageView];

//Start a timer
[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(changeImge) userInfo:nil repeats:YES];


animatedImageView.image = imagesToAnimate[0];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gallerySwiped)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imageGalleryView addGestureRecognizer:swipeGesture];

//Give the gallery a border
self.imageGalleryView.layer.borderColor = [UIColor grayColor].CGColor;
self.imageGalleryView.layer.borderWidth = 2.0f;
self.imageGalleryView.layer.cornerRadius = 15.0f;

现在可以按照预定时间更新图像

-(void)changeImge
{
    int index = [imagesToAnimate indexOfObject:animatedImageView.image];
    index++;

    //this is for the left to right animation ;
    CATransition *transition = [CATransition new];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;

    if (index > imagesToAnimate.count-1) {
        animatedImageView.image = imagesToAnimate [0];
    }else {
        animatedImageView.image = imagesToAnimate[index];
    }

    [animatedImageView.layer addAnimation:transition forKey:@"transition"];
}

这会处理滑动

-(void)gallerySwiped
{
    [self changeImge];
}

要停止计时器将其存储在全局变量中并调用invalidate。我希望这有帮助。