码迷,mamicode.com
首页 > 其他好文 > 详细

假设做一个循环滚动UIScrollView

时间:2015-09-11 17:27:49      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

先上效果图:

技术分享


首先初始化:

- (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

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/4801349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!