美文网首页
iOS开发-每日一记-自定义一个选项卡的View

iOS开发-每日一记-自定义一个选项卡的View

作者: 木_风 | 来源:发表于2016-06-10 09:28 被阅读470次

1.头文件定义block回调和让按钮模拟点击方法

#import

typedefvoid(^ClickBlock)(NSIntegerbuttonTag);

@interfaceSelectAnimationView :UIView

- (void)clikButtontag:(NSInteger)buttonTag;

@property(nonatomic,copy)ClickBlockblock;

@end

2.实现文件,初始化view 让按钮的下标线 点击做一个简单位移动画

#import"SelectAnimationView.h"

#define SPACINGWIDTH30

@implementationSelectAnimationView

- (instancetype)initWithFrame:(CGRect)frame{

self=[superinitWithFrame:frame];

if(self) {

[selfsetBackgroundColor:[UIColorblackColor]];

UIButton*videoSelectButton=[UIButtonbuttonWithType:UIButtonTypeCustom];

[videoSelectButtonsetTitle:@"视频"forState:UIControlStateNormal];

[videoSelectButtonsetTitleColor:[UIColororangeColor]forState:UIControlStateNormal];

[videoSelectButtonsetFrame:CGRectMake(SPACINGWIDTH,0, (VIEWWIDTH-2*SPACINGWIDTH)/3,VIEWHEIGT-2)];

videoSelectButton.tag=1;

[videoSelectButtonaddTarget:selfaction:@selector(clickButton:)forControlEvents:UIControlEventTouchUpInside];

[selfaddSubview:videoSelectButton];

UIView*lineView=[[UIViewalloc]initWithFrame:CGRectMake(SPACINGWIDTH,VIEWHEIGT-2, (VIEWWIDTH-2*SPACINGWIDTH)/3,2)];

lineView.tag=3;

[lineViewsetBackgroundColor:[UIColororangeColor]];

[selfaddSubview:lineView];

UIButton*photoSelectButton =[UIButtonbuttonWithType:UIButtonTypeCustom];

[photoSelectButtonsetTitle:@"照片"forState:UIControlStateNormal];

[photoSelectButtonsetTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];

photoSelectButton.tag=2;

[photoSelectButtonaddTarget:selfaction:@selector(clickButton:)forControlEvents:UIControlEventTouchUpInside];

[photoSelectButtonsetFrame:CGRectMake(VIEWWIDTH-(VIEWWIDTH-2*SPACINGWIDTH)/3-SPACINGWIDTH,0, (VIEWWIDTH-2*SPACINGWIDTH)/3,VIEWHEIGT)];

[selfaddSubview:photoSelectButton];

}

returnself;

}

- (void)clickButton:(UIButton*)button{

UIButton*videoButton=[selfviewWithTag:1];

UIButton*photoButton=[selfviewWithTag:2];

UIView*lineView=[selfviewWithTag:3];

if(button.tag==1) {

[UIViewanimateWithDuration:0.5animations:^{

[videoButtonsetTitleColor:[UIColororangeColor]forState:UIControlStateNormal];

[photoButtonsetTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];

[lineViewsetFrame:CGRectMake(SPACINGWIDTH,VIEWHEIGT-2, (VIEWWIDTH-2*SPACINGWIDTH)/3,2)];

}completion:^(BOOLfinished){

}];

}else{

[UIViewanimateWithDuration:0.5animations:^{

[photoButtonsetTitleColor:[UIColororangeColor]forState:UIControlStateNormal];

[videoButtonsetTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];

[lineViewsetFrame:CGRectMake(VIEWWIDTH-(VIEWWIDTH-2*SPACINGWIDTH)/3-SPACINGWIDTH,VIEWHEIGT-2, (VIEWWIDTH-2*SPACINGWIDTH)/3,2)];

}completion:^(BOOLfinished){

}];

}

_block(button.tag);

}

- (void)clikButtontag:(NSInteger)buttonTag;{

UIButton*clickButton=[selfviewWithTag:buttonTag];

[selfclickButton:clickButton];

}

@end

相关文章

网友评论

      本文标题:iOS开发-每日一记-自定义一个选项卡的View

      本文链接:https://www.haomeiwen.com/subject/pfdsdttx.html