做項(xiàng)目Demo的時(shí)候偶爾想到這個(gè)按鈕比較常用 , 就寫(xiě)出來(lái)了 , 可以寫(xiě)成工具類(lèi)方法給項(xiàng)目使用;
圖例
#import "RecommendViewCtrl.h"
@interface RecommendViewCtrl ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@end
@implementation RecommendViewCtrl
- (instancetype)init
{
self = [super init];
if (self)
{
self.view.backgroundColor = [UIColor whiteColor];
self.tabBarItem.title = @"推薦";
self.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/like@2x.png"];
self.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/like_selected@2x.png"];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.backgroundColor = [UIColor lightGrayColor];
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 5, self.view.bounds.size.height);
scrollView.delegate = self;
NSArray *colorArray = @[[UIColor redColor],[UIColor blueColor],[UIColor yellowColor],[UIColor lightGrayColor],[UIColor grayColor]];
for (int i = 0; i < 5; i++) {
[scrollView addSubview:({
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(scrollView.bounds.size.width * i, 0, scrollView.bounds.size.width, scrollView.bounds.size.height)];
// 1.左圖右文:
[view addSubview:({
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(100,50, 100, 100)];
button1.backgroundColor = [UIColor orangeColor];
[button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:@"左圖右文" forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
[button1 setImage:[UIImage imageNamed:@"icon.bundle/like_selected@2x.png"] forState:UIControlStateNormal];
[button1 setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
button1;
})];
// 2.左文右圖:
[view addSubview:({
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setFrame:CGRectMake(100, 150, 100, 100)];
button2.backgroundColor = [UIColor purpleColor];
[button2 setTitle:@"左文右圖" forState:UIControlStateNormal];
[button2.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
[button2 setImage:[UIImage imageNamed:@"icon.bundle/like_selected@2x.png"] forState:UIControlStateNormal];
// 文字偏移量:
[button2 setTitleEdgeInsets:UIEdgeInsetsMake(0, - button2.imageView.width - 10, 0, button2.imageView.width)];
// 圖片偏移量:
[button2 setImageEdgeInsets:UIEdgeInsetsMake(0, button2.titleLabel.width, 0, - button2.titleLabel.width)];
button2;
})];
// 3.上圖下文:
[view addSubview:({
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setFrame:CGRectMake(100, 250, 100, 100)];
button3.backgroundColor = [UIColor lightGrayColor];
[button3 setTitle:@"上圖下文" forState:UIControlStateNormal];
[button3.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
[button3 setImage:[UIImage imageNamed:@"icon.bundle/like_selected@2x.png"] forState:UIControlStateNormal];
// 文字偏移量:往下 , 往左移動(dòng):
[button3 setTitleEdgeInsets:UIEdgeInsetsMake(button3.imageView.height + 10, - button3.imageView.width, 0, 0)];
// 圖片偏移量:往上 , 往右移動(dòng):
[button3 setImageEdgeInsets:UIEdgeInsetsMake(0, button3.titleLabel.width / 2, button3.titleLabel.height + 10.0, - button3.titleLabel.width / 2)];
button3.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button3;
})];
// 4.上文下圖:
[view addSubview:({
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setFrame:CGRectMake(100, 350, 100, 100)];
button4.backgroundColor = [UIColor greenColor];
[button4 setTitle:@"上圖下文" forState:UIControlStateNormal];
[button4.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
[button4 setImage:[UIImage imageNamed:@"icon.bundle/like_selected@2x.png"] forState:UIControlStateNormal];
// 文字偏移量:往上 , 往左移動(dòng):
[button4 setTitleEdgeInsets:UIEdgeInsetsMake(- button4.imageView.height + 10, - button4.imageView.width, 0, 0)];
// 圖片偏移量:往下 , 往右移動(dòng):
[button4 setImageEdgeInsets:UIEdgeInsetsMake(button4.titleLabel.height + 10, button4.titleLabel.width / 2, - button4.titleLabel.height - 10, - button4.titleLabel.width / 2)];
button4;
})];
view.backgroundColor = [colorArray objectAtIndex:i];
view;
})];
}
scrollView.pagingEnabled = YES;
[self.view addSubview:scrollView];
}
- (void)buttonClicked:(UIButton *)button
{
NSLog(@"buttonClicked");
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
// NSLog(@"scrollViewDidScroll - %@",@(scrollView.contentOffset.x));
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"BeginDragging");
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"EndDragging");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}
@end