IOS开发:使用animateWithDuration简单地控制页面切换效果

在本例子中,使用一个按钮切换两个view视图,这两个视图上面仅仅放置两张图片,当单击按钮时,第一个视图消失,第二个视图淡入屏幕,在显示完成后立马向右下角移动。

1.新建工程,建个viewbase application即可。

另外在xib文件中,添加两个view1,view2,以及在每个view上添加一个UIImageView,并且放置两张图片。在放置一个按钮。效果如下:


2.对应需要添加地controller文件的代码如下:

ViewPageDemoViewController.h代码如下:

#import <UIKit/UIKit.h>

@interface ViewPageDemoViewController : UIViewController
{
    IBOutlet UIView *view1;
    IBOutlet UIView *view2;
}
@property (nonatomic,retain) IBOutlet UIView *view1;
@property (nonatomic,retain) IBOutlet UIView *view2;

-(IBAction)changePage:(id)sender;

@end


ViewPageDemoViewController.m代码如下:

//
//  ViewPageDemoViewController.m
//  ViewPageDemo
//
//  Created by kumahikarihui Baxiaxx on 12-7-10.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewPageDemoViewController.h"

@implementation ViewPageDemoViewController
@synthesize view1,view2;

-(void)animationShow:(NSInteger)index
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0f];
    switch (index) {
        case 0:
            [UIView animateWithDuration:1 animations:^{
                view2.alpha = 1.0;
                view1.alpha = 0.0;
            }completion:^(BOOL finished){
                [UIView animateWithDuration:4.0
                                 animations:^{
                                     view2.center = CGPointMake(500.0, 470.0);
                                     view1.center = CGPointMake(160.0, 230.0);
                                 }];
            }];
            break;
        case 1:
            [UIView animateWithDuration:1 animations:^{
                view1.alpha = 1.0;
                view2.alpha = 0.0;
            }completion:^(BOOL finished){
                [UIView animateWithDuration:4.0
                                 animations:^{
                                     view1.center = CGPointMake(500.0, 470.0);
                                     view2.center = CGPointMake(160.0, 230.0);
                                 }];
            }];
            break;
        default:
            break;
    }
}

-(IBAction)changePage:(id)sender
{
    static BOOL bAgain = TRUE;
    if(bAgain)
    {
        [view1 removeFromSuperview];
        [self.view addSubview:view2];
        [self animationShow:0];
    }
    else
    {
        [view2 removeFromSuperview];
        [self.view addSubview:view1];
        [self animationShow:1];
    }
    bAgain = !bAgain;
}
-(void)viewDidLoad
{
    view2.alpha = 0.0;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void)dealloc
{
    [view2 release];
    [view1 release];
    [super dealloc];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
这里有一个小小的问题,运行程序会发现,第一次单击按钮时,第二图片并没有淡入的效果,而是直接就显示出来了,这是由于淡入淡出的效果只用的是alpha从0到1之前慢慢过渡而实现的,仔细看代码发现其实第一次按钮显示第二张图的时候,view2.alpha的值本来就为1,所以从1变化到1当然没有效果了,解决办法很简单,只要预先初始化一下就可以了(将viewDidLoad()中绿色注释的代码打开即可)。












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值