首页 > 代码库 > 如果做一个循环滚动的UIScrollView
如果做一个循环滚动的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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。