新手自定义背景滑动特效选项条,超简单实现方法
在很多APP中,我们都可以看见那些特效绚丽的滑动选项条,那么如何才能够简单,快速的实现那样的效果呢?首先我们分析一下那样的控件都需要什么?
1、既然是选项条,必然需要选择,因此我们需要UIButton。
2、选择那个button,那个button的背景从之前选择的那个平滑的移动到当前选择的button上,因此我们不能单纯的使用button的背景色,我们需要一个转为提供颜色的view,这样才有可能达到视图的平滑移动。
3、我们这些view需要一个载体,进行同意管理,因此我们需要一个MainView,来承载这些控件。
好了,分析完了,我们动手吧。
第一步:我们需要定义一些我们需要的对象,某些变量需要全局使用,定义的时候根据需要在权衡。
- (void)myButtonClcik:(id)sender{// NSString *selectedBtn = [NSString stringWithFormat:@"%@",[titleArray objectAtIndex:button.tag]];// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:selectedBtn delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];// [alert show]; //添加动画过度效果 [UIView beginAnimations:@"slowGlide" context:nil]; [UIView setAnimationDuration:0.3f]; //设置每次只能选择一个button UIButton *button = (UIButton *)sender; if(!button.selected){ for (UIButton *eachBtn in btnArray) { if(eachBtn.isSelected){ [eachBtn setSelected:NO]; } } [button setSelected:YES]; //设置点击那个按钮,那个按钮的背景改变为backView的颜色 [backView setFrame:button.frame]; } [UIView commitAnimations];}此种方法简单,但是可能有些地方不合理,望高手指点。
demo 下载地址:http://download.csdn.net/detail/zyc851224/4492735