2016-04-11 7 views
1

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; 

} 

答えて

0

ScrollViewUIViewを追加し、このviewにあなたのすべてのものを追加します。 アニメーションをviewまたはview's layerに追加します。これがうまくいくと思っています。アニメーションがとても

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

bounds.origin.y -= 100; 

この行を追加した後:)あなたが戻ってあなたの最初の位置をしたい

場合は、あなたのviewDidLoadの最後の5行は、以下のようにすべき

bounds.origin.y += 100; 

animation.toValue =[NSValue valueWithCGRect:bounds]; 

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

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

実際に私はビューでscollviewを追加しました[self.view addSubview:scrollview]; –

+0

はスクロールビューに別のビューを追加するようになりました。 [scrollview addsubview:newView]のように。ツリー、車、草などのすべての画像ビューをこの新しいViewに追加します。このnewViewにスクロールビューの代わりにアニメーションを与えます。 – Lion

+0

あなたはどんなアニメーションが必要ですか?あなたのコードはある種のアニメーションも実行します。私が示唆しているのは、標準的で簡単な方法です。 – Lion

関連する問題