IOS 轮播初步学习
发布日期:2021-11-21 04:40:55
浏览次数:31
分类:技术文章
本文共 4071 字,大约阅读时间需要 13 分钟。
(PS:IOS新手,写的东西难免有错,请大家指正)
1.自定义一个类,用于展现轮播图片,该类叫IKEDWheelPic,该类实现了UIScrollView的继承,其中pics存的都IKEDItemInfo是类型的,暂时如下
//// IKEDWheelPic.h// lunbo//// Created by apple on 14-2-26.// Copyright (c) 2014年 com.tyust. All rights reserved.//#import2.理解图片循环播放有两个点,第一个点图片由最后一张继续向前滑动,应该到第一张,比如共有4张图片,继续往前拉,她的centerpoint应该等于第一幅图的center,从第一张往后拉,应该是往前移动两个界面宽度,这点理解了,基本上解决了这个问题了,代码如下@interface IKEDWheelPic : UIScrollView{ //轮播里面放的就是一个一个的按钮,按钮上填充一个UIImageView UIButton *pic; BOOL flag; int scrollTopicFlag; //用于处理时间的对象 NSTimer *scrollTimer; int currentPage; CGSize imageSize; UIImage *image;}@property(nonatomic,strong)NSArray * pics;@end
//// IKEDWheelPic.m// lunbo//// Created by apple on 14-2-26.// Copyright (c) 2014年 com.tyust. All rights reserved.//#import "IKEDWheelPic.h"#import "IKEDItemInfo.h"@implementation IKEDWheelPic- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.frame = frame; [self setSelfProperties]; } return self;}-(void)drawRect:(CGRect)rect{ [self setSelfProperties];}-(void)setSelfProperties{ self.pagingEnabled = YES; self.scrollEnabled = YES; self.showsHorizontalScrollIndicator = NO; self.showsVerticalScrollIndicator = NO; self.backgroundColor = [UIColor whiteColor]; //实现ScrollowView的delegate方法 self.delegate = self;}-(void)showPic{ NSMutableArray *tempImageArray = [[NSMutableArray alloc]init]; //将传过来的数组最后的一张图片纺织再第一张的前面 [tempImageArray addObject:[self.pics lastObject]]; for (id obj in self.pics) { [tempImageArray addObject:obj]; } [tempImageArray addObject:[self.pics objectAtIndex:0]]; //清空原来的数组 self.pics = Nil; //将可变数组的内容付给原来的数组 self.pics = tempImageArray; //关键的地方 int i = 0; for(id obj in self.pics) { //每次调用会清空原来的展现的Button pic = nil; //自定义Button pic = [UIButton buttonWithType:UIButtonTypeCustom]; //UIViewContentModeTop:The option to center the content aligned at the top in the view’s bounds. pic.imageView.contentMode = UIViewContentModeTop; //根据图片的次序来设置Button的位置 [pic setFrame:CGRectMake(i*self.frame.size.width,0, self.frame.size.width, self.frame.size.height)]; UIImageView * tempImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, pic.frame.size.width, pic.frame.size.height)]; tempImage.contentMode = UIViewContentModeScaleAspectFill; [tempImage setClipsToBounds:YES]; [tempImage setImage:[obj objectForKey:@"pic"]]; [pic addSubview:tempImage]; [pic setBackgroundColor:[UIColor grayColor]]; pic.tag = i; [self addSubview:pic]; //用于展示标签栏 UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(i*self.frame.size.width, self.frame.size.height-30, self.frame.size.width,30)]; [title setBackgroundColor:[UIColor blackColor]]; [title setAlpha:.7f]; [title setText:[NSString stringWithFormat:@" %@",[obj objectForKey:@"title"]]]; [title setTextColor:[UIColor whiteColor]]; [title setFont:[UIFont fontWithName:@"Helvetica" size:12]]; [self addSubview:title]; i ++; } //scrollow view的大小 [self setContentSize:CGSizeMake(self.frame.size.width*[self.pics count], self.frame.size.height)]; [self setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];}- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat Width=self.frame.size.width; if (scrollView.contentOffset.x == self.frame.size.width) { flag = YES; } if (flag) { if (scrollView.contentOffset.x <= 0) { [self setContentOffset:CGPointMake(Width*([self.pics count]-2), 0) animated:NO]; }else if (scrollView.contentOffset.x >= Width*([self.pics count]-1)) { [self setContentOffset:CGPointMake(Width*1, 0) animated:NO]; } } currentPage = scrollView.contentOffset.x/self.frame.size.width-1;}@end
转载地址:https://blog.csdn.net/xiaoji94shen/article/details/19978775 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月08日 13时55分35秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
推荐阅读的多核编程技术书籍
2019-04-27
维基百科上的算法和数据结构链接很强大
2019-04-27
选择排序
2019-04-27
PHP session回收机制
2019-04-27
最新的全球编程语言,操作系统,web服务器等使用率分析报告
2019-04-27
用C语言写PHP扩展
2019-04-27
PHP Extension programming
2019-04-27
海量数据处理
2019-04-27
PHP防止注入攻击
2019-04-27
多路IO复用模型 select epoll 等
2019-04-27
Linux Epoll介绍和程序实例
2019-04-27
output_buffering详细介绍
2019-04-27
php缓冲 output_buffering和ob_start
2019-04-27
php error_reporting 详解
2019-04-27
剖析PHP中的输出缓冲
2019-04-27
HTTP响应头不缓存
2019-04-27
PHP安装扩展mcrypt以及相关依赖项 【PHP安装PECL扩展的方法】
2019-04-27
Javascript到PHP加密通讯的简单实现
2019-04-27
德国SNS交友/视频网站Poppen.de的技术架构分享
2019-04-27
UNIX环境编程
2019-04-27