iOS纯代码制作欢迎界面——UIScrollView, UIPageControl, UIImageView,UIButton, NSTimer
欢迎界面,还是比较简单的,一个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的更多相关文章
- iOS纯代码工程手动快速适配
首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...
- iOS纯代码手动适配 分类: ios技术 2015-05-04 17:14 239人阅读 评论(0) 收藏
首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...
- 心跳(纯代码制作心形,animation动画)
思路:利用两个长方形(比例是2:3 | 3:2)可以合成心形,然后利用动画,缩放大小实现心跳(纯代码),效果如下: <body> <div></div> </ ...
- ios - 纯代码创建collectionView
开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...
- UIScrollView,UIPageControl,UIImageView 实现图片轮播的效果
上一篇博客介绍了如何将XCode创立的项目提交到Git版本控制,这次就直接做一个图片轮播的展示demo,刚好可以把UIScrollView.UIPageControl.UIImageView这三个控件 ...
- iOS 用代码搭建UI界面实例
1.背景 学习IOS开发也差不多两个月了,赶鸭子上架的学习模式让我学习比较快,但是真心很累,每天有每天的工作进度,在学习的 时候需要边做一个项目真心有点累,但是看到自己的收获还是值得的.自己原来是做C ...
- Object-C iOS纯代码布局 一堆代码可以放这里!
前言: 最近写的文章都是创业类,好吧,今天好好写写技术类的文章! 不过分享的不是IOS相关的文章,毕竟这几天在速成IOS,看的是object-c,由于速成的很快,好累! 好在现在基本已经入了点门道了, ...
- Objective-C iOS纯代码布局 一堆代码可以放这里!
前言: 最近写的文章都是创业类,好吧,今天好好写写技术类的文章! 不过分享的不是IOS相关的文章,毕竟这几天在速成IOS,看的是objective-c,由于速成的很快,好累! 好在现在基本已经入了点门 ...
- iOS纯代码适配masonry中mas_的问题
//equalto 和 mas_equalto 是有区别的.但是我们不打算去了解,可以通过添加以下代码来统一. //注意!! 宏定义必须要放在 import 引入头文件之前! //define thi ...
随机推荐
- Android Studio 连接真机调试
以小米4为例,先将手机通过USB连接电脑,在设备管理器中确保驱动安装正确. 对手机的设置 1.设置手机为开发者模式(设置->关于手机->连续点击MIUI版本--开启成功) 2.在更多设置中 ...
- bluehost 邮箱设置问题
问题描述: e-elitech.com域名,elitechus.com域名均在阿里云注册,en.e-elitech.com解析到bluehost虚拟主机,www.elitechus.com也解析到bl ...
- P图
照片名称:调出照片柔和的蓝黄色-简单方法,1.打开原图素材,按Ctrl + J把背景图层复制一层,点通道面板,选择蓝色通道,图像 > 应用图像,图层为背景,混合为正片叠底,不透明度50%,反相打 ...
- IPSec VPN实验
IPSec VPN实验 实验拓扑: 实验目的:掌握IPSec VPN原理 掌握site-to-site VPN配置 IPSec配置参数: IKE policy isakmp key 转换集 加密算法 ...
- stock 仓位
别胆大求多,不轻信盲从.抓住几只自己长期关注并看好的股票.将这几只股票选为自选股,而其他股,不管是机构推荐还是股评荐股,都要谨慎,不轻易听从. 巧用“三三制”,根据趋势控制仓位.当不知道是在涨还是在跌 ...
- Spring + iBATIS完整示例
最近研究了一下Spring + iBATIS.发现看别人的例子是一回事,自己写一个完整的应用又是另外一回事.自己受够了网上贴的一知半解的代码. iBATIS是一个持久化框架,封面了sql过程,虽然sq ...
- dom4j解析xml实例
dom4j是一个java的XML API,类似jdom,用来读写XML文件,它性能优异.功能强大和极易使用等特点 所用jar包:dom4j-1.6.1.jar 需要解析的xml文件:people.xm ...
- UILabel 详解
转自:http://blog.csdn.net/zhaopenghhhhhh/article/details/16331041 ·UILable是iPhone界面最基本的控件,主要用来显示文本信息.· ...
- linux下sed命令对文件执行文本替换
让我们看一下 sed 最有用的命令之一,替换命令.使用该命令,可以将特定字符串或匹配的规则表达式用另一个字符串替换.下面是该命令最基本用法的示例: $ sed -e ‘s/foo/bar/’ myfi ...
- ERROR security.UserGroupInformation
[java] 15/11/14 12:58:19 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. ...