标签:
欢迎界面,还是比较简单的,一个UIScrollView控件,一个UIPageControl,几个UIImageView即可摆平。在这里光玩这些,就显得诚意不足了。特意拓展一下,再加几个UIButton,可以让这个欢迎界面变成可点击的,可滑动的模块添加在页面中,然后再加一个NSTimer,让它自己隔2秒自己循环滑动着,立马让它变成可以放在主页用于展示主打商品的模块。
下面直接展示可直接运行的Demo,代码都挺简单,这次就不添加注解了。
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong) UIScrollView *scrollerView;
@property(nonatomic,strong) NSArray *images;
@property(nonatomic,strong) NSMutableArray *imageButtons;
@property(nonatomic,strong) UIPageControl *pageControl;
@end
@implementation ViewController
-(NSArray *)images{
if (!_images) {
_images = @[@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png"];
}
return _images;
}
-(NSMutableArray *)imageButtons{
if (!_imageButtons) {
_imageButtons = [NSMutableArray array];
}
return _imageButtons;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initScrollerView];
}
-(void)initScrollerView{
UIScrollView *scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.scrollerView = scrollerView;
scrollerView.contentSize = CGSizeMake(self.view.bounds.size.width * self.images.count, self.view.bounds.size.height);
scrollerView.pagingEnabled = YES;
scrollerView.bounces = NO;
scrollerView.delegate = self;
[self.view addSubview:scrollerView];
for (int i = 0; i<self.images.count; i++) {
UIImage *image = [UIImage imageNamed:self.images[i]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(self.view.bounds.size.width * i, 0, self.view.bounds.size.width, self.view.bounds.size.height);
imageView.contentMode = UIViewContentModeScaleToFill;
[scrollerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(self.view.bounds.size.width * i, 0, self.view.bounds.size.width, self.view.bounds.size.height);
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:@selector(clickScrollerViewButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollerView addSubview:button];
[self.imageButtons addObject:button];
}
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scrollerView.bounds.size.height - 20, scrollerView.bounds.size.width, 10)];
self.pageControl = pageControl;
pageControl.numberOfPages = self.images.count;
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
pageControl.userInteractionEnabled = NO;
[pageControl addTarget:self action:@selector(clickScrollerViewButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pageControl];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(changeImageByTimer) userInfo:nil repeats:YES];
[timer fireDate];
}
-(void)clickScrollerViewButton:(UIButton*)button{
NSInteger num = [self.imageButtons indexOfObject:button];
NSLog(@"%ld",(long)num);
}
-(void)changeImageByTimer{
self.pageControl.currentPage = (self.pageControl.currentPage+1)%self.images.count;
self.scrollerView.contentOffset = CGPointMake(self.pageControl.currentPage * self.view.bounds.size.width, 0);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint point = scrollView.contentOffset;
NSInteger index = point.x / scrollView.frame.size.width;
self.pageControl.currentPage = index;
}
@end
iOS纯代码制作欢迎界面——UIScrollView, UIPageControl, UIImageView,UIButton, NSTimer
标签:
原文地址:http://www.cnblogs.com/yyt-hehe-yyt/p/4732066.html