ScrollView 内容中的动画

Animation in content of ScrollView

我想为 scrollview 的内容制作动画,我已经尝试过这个动画。

- (void)viewDidLoad {
    [super viewDidLoad];

    //Scroll View Created Programmatically
    scrollview=[[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollview.delegate=self;
    scrollview.contentSize=CGSizeMake(scrollview.bounds.size.width, self.view.bounds.size.height);
    [self.view addSubview:scrollview];

    // Road Image Created Programmatically
    backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    backgroundImageView.image = [UIImage imageNamed:@"roadds.png"];
    backgroundImageView.contentMode = UIViewContentModeRedraw;
    [scrollview addSubview:backgroundImageView];

    // Tree top left Created Programmatically
    Tree = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 30, 50)];
    Tree.image = [UIImage imageNamed:@"rsz_1rsz_tree.png"];
    Tree.contentMode = UIViewContentModeTopLeft;
    [scrollview addSubview:Tree];

    //Tree top Right
    Tree1 = [[UIImageView alloc] initWithFrame: CGRectMake(self.view.frame.size.width-30, 0, 30, 50)];
    Tree1.image = [UIImage imageNamed:@"rsz_tree1.png"];
    Tree1.contentMode = UIViewContentModeTopRight;
    [scrollview addSubview:Tree1];

    //Tree Bottom left
    Tree2 = [[UIImageView alloc] initWithFrame: CGRectMake(0, self.view.frame.size.height-80, 30, 50)];
    Tree2.image = [UIImage imageNamed:@"rsz_tree2.png"];
    Tree2.contentMode = UIViewContentModeBottomLeft;
    [scrollview addSubview:Tree2];

    //Tree Bottom Right
    Tree3 = [[UIImageView alloc] initWithFrame: CGRectMake(self.view.frame.size.width-30, self.view.frame.size.height-80, 30, 50)];
    Tree3.image = [UIImage imageNamed:@"rsz_tree3.png"];
    Tree3.contentMode = UIViewContentModeBottomRight;
    [scrollview addSubview:Tree3];

    // Car
    car = [[UIImageView alloc] initWithFrame: CGRectMake((self.view.frame.size.width)/2+20, self.view.frame.size.height-120, 40, 60)];
    car.image = [UIImage imageNamed:@"rsz_car1.png"];
    car.contentMode = UIViewContentModeCenter;
    [scrollview addSubview:car];

    //Grass
    grass = [[UIImageView alloc] initWithFrame:CGRectMake(0,  (self.view.frame.size.height)/2,30, 30)];
    grass.image = [UIImage imageNamed:@"rsz_grass.png"];
    grass.contentMode = UIViewContentModeBottomLeft;
    [scrollview addSubview:grass];

    //Grass
    grass1 = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-60,   (self.view.frame.size.height)/2,30, 30)];
    grass1.image = [UIImage imageNamed:@"rsz_grass1.png"];
    grass1.contentMode = UIViewContentModeBottomLeft;
    [scrollview addSubview:grass1];

    // Left Arrow
    left = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width)/2+60,  (self.view.frame.size.height-35),30, 30)];
    [left setBackgroundImage:[UIImage imageNamed:@"rsz_arrowl.png"]forState:UIControlStateNormal];
    left.contentMode = UIViewContentModeBottomRight;
    [scrollview addSubview:left];

    //right arrow
    right = [[UIButton alloc] initWithFrame:CGRectMake((self.view.bounds.size.width)/2 - 90,  (self.view.frame.size.height-35),30, 30)];
    [right setBackgroundImage:[UIImage imageNamed:@"rsz_arrowr.png"]forState:UIControlStateNormal];
    right.contentMode = UIViewContentModeBottomLeft;
    [scrollview addSubview:right];

    // For Animation
    CGRect bounds = scrollview.bounds;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    animation.duration = 10;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.fromValue = [NSValue valueWithCGRect:bounds];

    bounds.origin.y += 100;
    animation.toValue =[NSValue valueWithCGRect:bounds];

    [scrollview.layer addAnimation:animation forKey:@"bounds"];

    scrollview.bounds = bounds;

}

UIView 添加到 ScrollView,然后将所有内容添加到此 view。 并为此 viewview's layer 添加动画。希望这会奏效。 :)

如果您想在动画播放后返回初始位置,请添加此行

bounds.origin.y -= 100;

之后
[scrollview.layer addAnimation:animation forKey:@"bounds"];

所以,您的 viewdidLoad 的最后五行应该如下所示,

bounds.origin.y += 100;

animation.toValue =[NSValue valueWithCGRect:bounds];

[scrollview.layer addAnimation:animation forKey:@"bounds"];

bounds.origin.y -= 100;
scrollview.bounds = bounds;