每当我们同一个页面切换不同数据的时候 通常使用hidden 的来隐藏和显示,这样用户感觉很生硬,
经常我们会看见一些应用,切换数据的时候有个渐变的过程,这样个用户的感觉更加平滑,废话不多说了
例子很简单,用了两个view 上面展示不同的数据 ,一个按钮来切换视图显示不同数据
- (void)viewDidLoad {
[super viewDidLoad];
oneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:oneView];
UILabel *testLabel = [[UILabel alloc]init];
testLabel.frame =CGRectMake(100, 100, 200, 40);
testLabel.text = @"这个世界真的很美好";
testLabel.backgroundColor = [UIColor clearColor];
[oneView addSubview:testLabel];
oneView.alpha = 0;
twoView= [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:twoView];
UILabel *testLabel2 = [[UILabel alloc]init];
testLabel2.frame =CGRectMake(100, 100, 200, 40);
testLabel2.text = @"这个世界还可以哦";
testLabel2.backgroundColor = [UIColor clearColor];
[twoView addSubview:testLabel2];
UIButton *testoneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testoneButton.frame = CGRectMake(100, 200, 100, 100);
[testoneButton setTitle:@"切克闹" forState:UIControlStateNormal];
[testoneButton addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];
[twoView addSubview:testoneButton];
}
主要是在这 在一个动画里来控制数据切换是是否透明,是不是很简单,这样用户体验好了很多 ,命名很随便真的 抱歉,趁服务宕机的时间断分享了这个小技巧!
-(void)testAction
{
[UIView animateWithDuration:0.6f animations:^(void){
oneView.alpha = 1.0;
twoView.alpha = 0.0;
} completion:^(BOOL finised){
}];
}
<a target=_blank href="http://download.youkuaiyun.com/detail/lengshengren/7982197" target="_blank">demo 下载地址</a>