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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容