假设做一个循环滚动UIScrollView
先上效果图:
首先初始化:
- (void)viewDidLoad
{
//加入最后一张图 用于循环
int length = 4;
NSMutableArray *tempArray = [NSMutableArray array];
for (int i = 0 ; i < length; i++)
{ NSString* str = [NSString stringWithFormat:@"title%d",i];
[tempArray addObject:str];
} NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:length+2]; if (length > 1)
{
NSString *str1 = [tempArray objectAtIndex:length-1]; //title3
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str1 tag:-1];
[itemArray addObject:item];
} for (int i = 0; i < length; i++)
{
NSString *str2 = [tempArray objectAtIndex:i];
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str2 tag:i];
[itemArray addObject:item]; } //加入第一张图 用于循环
if (length >1)
{
NSString *str3 = [tempArray objectAtIndex:0];
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str3 tag:length]; //title0
[itemArray addObject:item];
}
ViewFrame *bannerView = [[ViewFrame alloc] initWithFrame:CGRectMake(0, 0, 320, 105) imageItems:itemArray isAuto:NO]; [self.view addSubview:bannerView]; }
SGFocusImageItem仅仅有一个Title和tag:
@interface SGFocusImageItem : NSObject @property (nonatomic, strong) NSString *title;
@property (nonatomic, assign) NSInteger tag; - (id)initWithTitle:(NSString *)title tag:(NSInteger)tag; @end
主要是ViewFrame:
#define ITEM_WIDTH 320.0
@interface ViewFrame : UIView<UIScrollViewDelegate> @property(nonatomic,strong)NSMutableArray* imageItems;
@property(nonatomic,strong)UIScrollView* scrollView; - (id)initWithFrame:(CGRect)frame imageItems:(NSArray *)items isAuto:(BOOL)isAuto;
@end
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
float targetX = _scrollView.contentOffset.x;
NSLog(@"=====scrollViewdidscroll targetX=%f",targetX); <strong> if ([_imageItems count] >= 3) {
//假设超过最后一张图,则又一次设置setContentOffset
if (targetX >= ITEM_WIDTH * ([_imageItems count]-1)) {
targetX = ITEM_WIDTH;
[_scrollView setContentOffset:CGPointMake(targetX, 0) animated:NO];
}
else if(targetX <= 0){
targetX = ITEM_WIDTH * ([_imageItems count] - 2);
[_scrollView setContentOffset:CGPointMake(targetX, 0) animated:NO];
}
}</strong>
} - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} -(void)setupViews{
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.scrollsToTop = NO; CGSize size = CGSizeMake(320, 0);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * _imageItems.count,
_scrollView.frame.size.height);
[self addSubview:_scrollView];
for (int i = 0; i < _imageItems.count; i++) {
SGFocusImageItem* item = [_imageItems objectAtIndex:i];
int x = i * _scrollView.frame.size.width;
int width = _scrollView.frame.size.width;
int height = _scrollView.frame.size.height; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, width, height)];
if (i == 0) {
imageView.backgroundColor = [UIColor brownColor];
} else if(i == 1) {
imageView.backgroundColor = [UIColor orangeColor];
} else if(i == 2) {
imageView.backgroundColor = [UIColor blueColor];
}else if(i == 3){
imageView.backgroundColor = [UIColor redColor];
}else if(i == 4){
imageView.backgroundColor = [UIColor greenColor];
}else if(i == 5) {
imageView.backgroundColor = [UIColor lightGrayColor];
} UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(x+160, 30, 50, 50)];
field.text = item.title;
// NSLog(@"====kkkkk title=%@",item.title);
field.textAlignment = UITextAlignmentCenter;
field.font = [UIFont systemFontOfSize:18]; [_scrollView addSubview:imageView];
[_scrollView addSubview:field]; } } - (id)initWithFrame:(CGRect)frame imageItems:(NSArray *)items isAuto:(BOOL)isAuto{
self = [super initWithFrame:frame];
if (self) {
_imageItems = [NSMutableArray arrayWithArray:items];
[self setupViews]; }
return self;
}
代码能够在http://download.csdn.net/detail/baidu_nod/7679089下载
版权声明:本文博主原创文章,博客,未经同意不得转载。
假设做一个循环滚动UIScrollView的更多相关文章
- 假设做一个精美的Login界面(攻克了一EditText自带clear的功能,相似iphone的UITextField)
先上图: XML为: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- Application_Start事件中用Timer做一个循环任务
protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new Syste ...
- APP中常见上下循环滚动通知的简单实现,点击可进入详情
APP中常见上下循环滚动通知的简单实现,点击可进入详情 关注finddreams博客,一起分享一起进步!http://blog.csdn.net/finddreams/article/details/ ...
- Jquery制作--循环滚动列表
自己模仿JQ插件的写法写了一个循环滚动列表插件,支持自定义上.下.左.右四个方向,支持平滑滚动或者间断滚动两种方式,都是通过参数设置.JQ里面有些重复的地方,暂时没想到更好的方法去精简.不过效果还是可 ...
- UIScrollView 循环滚动,代码超简单
如今非常多应用里面多多少少都用到了循环滚动,要么是图片.要么是view,或者是其它,我总结一下,写了个demo分享给大家. 先看代码之后在讲原理: 1.创建一个空的项目(这个我就不多说了). 2.加入 ...
- UIScrollView循环滚动1
现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...
- 使用UIScrollView和UIPageControl做一个能够用手势来切换图片的效果
利用UIScrollView的滚动效果来实现,先上图: 实现过程是:在viewController里先增加UIScrollView和UIPageControl: -(void) loadView { ...
- 使用UIScrollView 结合 UIImageView 实现图片循环滚动
场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...
- UIScrollView实现自动循环滚动广告
实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...
随机推荐
- C++ BYTE数组转字符串
第一种情况: BYTE[0]=Ox12 BYTE[1]=0x34 BYTE[2]=0x56 最后要转换成字符串123456 另外一种情况: BYTE[0]=Ox12 BYTE[1]=0x34 BYTE ...
- workerman-chat(PHP开发的基于Websocket协议的聊天室框架)(thinkphp也是支持socket聊天的)
workerman-chat(PHP开发的基于Websocket协议的聊天室框架)(thinkphp也是支持socket聊天的) 一.总结 1.下面链接里面还有一个来聊的php聊天室源码可以学习 2. ...
- Python 标准库和第三方库的安装位置、Python 第三方库安装的各种问题及解决
首先使用 sys 下的 path 变量查看所有的 python 路径: import sys sys.path 标准库 lib 目录下(home 目录/pythonXX.XX/lib) 第三方库 在 ...
- MouseGestureL.ini shift up/down/left/right edge
MouseGestureL.ini [ShiftPress]Icon=C:\Windows\System32\explorer.exe,6Custom=GetKeyState("Shift& ...
- JS表格分页组件:fupage的设计思路和具体用法(未来考虑开源,争取在2015年)
一.背景 之前在秒针工作的时候,某js高级工程师写了很多自己的组件,其中一套是分页组件,叫做st-grid.不过在我看来,bug太多,我经常给他反馈bug,我也不清楚为啥别人没有发现. ...
- .NET Core微服务之路:不断更新中的目录 (v0.43)
原文:.NET Core微服务之路:不断更新中的目录 (v0.43) 微服务架构,对于从事JAVA架构的童鞋来说,早已不是什么新鲜的事儿,他们有鼎鼎大名的Spring Cloud这样的全家桶框架支撑, ...
- [Ramda] Change Object Properties with Ramda Lenses
In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus ...
- [React] displayName for stateless component
We can use 'displayName' on component to change its component tag in dev tool: import React from 're ...
- Oracle 11g对依赖的推断达到字段级
在Oracle 10g下,推断依赖性仅仅达到了对象级.也就是说存储过程訪问的对象一旦发生了变化.那么Oracle就会将存储过程置为INVALID状态.所以在为表做了DDL操作后.须要把存储过程又一次进 ...
- NSString与int和float的相互转换
NSString *tempA = @"123"; NSString *tempB = @"456"; 1,字符串拼接 NSString *newString ...