图片自动轮播

ios 使用UIScrollView图片自动轮播
思路:建立一个工具类,以后方便在其他地方使用

使用方法:
在viewDidLoad位置获取得到异常的frame值
在viewDidAppear位置获取得到正确的frame值

-(void) autoScrollerView{
    AutoScrollerView *autoScrollerView = [[AutoScrollerView alloc] initWithFrame:self.view.bounds];
    //setPoitWidthAndHeigth 设置点的大小,withPointBottomMargin距离底部的距离,widthPointMargin点的间距
    [autoScrollerView setPoitWidthAndHeigth:10 withPointBottomMargin:40 widthPointMargin:25];
    NSArray *array = @[@"mainmenubackground.jpg",@"mainmenubackground.jpg",@"mainmenubackground.jpg",@"mainmenubackground.jpg"];

    [autoScrollerView setImageNames:array withPointSelectColor:[UIColor yellowColor] withPointNortalColor:[UIColor blueColor]];
    [autoScrollerView setAutoScroller:YES TimeInterval:3.0f]; //设置是自动轮播和时间为3秒
    [self.view addSubview:autoScrollerView];
}

AutoScrollerView.h文件

#import <UIKit/UIKit.h>

@interface AutoScrollerView : UIView<UIScrollViewDelegate>


-(void) setImageNames:(NSArray *)array withPointSelectColor:(UIColor *)selectColor withPointNortalColor:(UIColor *)nortalColor;

-(id) setPoitWidthAndHeigth:(int) WidthHeigth withPointBottomMargin:(int) marginToBottom widthPointMargin:(int) marginMax;
-(void) setAutoScroller:(Boolean )flag TimeInterval:(float) interval;
@end

AutoScrollerView.m文件

#import "AutoScrollerView.h"
#import "LJTool.h"

@implementation AutoScrollerView
{
    int widthAndHeigth;
    int marginBottom;
    int marginMaxWidth;
    UIScrollView *pageScrollView;
    int pointCount;
    UIColor *pointSelectColor;
    UIColor *pointNortalColor;
     NSTimer *aotuScrollTimer;
    Boolean AutoScroller;
    float timeInterval;

}

-(void) setImageNames:(NSArray *)array withPointSelectColor:(UIColor *)selectColor withPointNortalColor:(UIColor *)nortalColor{

    pointCount =(int)array.count;
    int AllWidth = widthAndHeigth* pointCount;
    int AllMargin = self.frame.size.width - AllWidth;
    int marginWidth = AllMargin/pointCount;
    if(marginWidth > marginMaxWidth){
        marginWidth = marginMaxWidth;
    }
    int marginStart = (self.frame.size.width - AllWidth - (marginWidth * (pointCount - 1)))/2;
    [pageScrollView setContentSize:CGSizeMake(self.frame.size.width * pointCount, self.frame.size.height)];
    pointSelectColor = selectColor;
    pointNortalColor = nortalColor;
    for (int i = 0; i < array.count; i++) {

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        [imageView setImage:[UIImage imageNamed:array[i]]];
        [pageScrollView addSubview:imageView];

        UIView *point = [[UIView alloc] initWithFrame:CGRectMake(marginStart+(marginWidth+widthAndHeigth)*i, self.frame.size.height - widthAndHeigth - marginBottom, widthAndHeigth, widthAndHeigth)] ;
        [LJTool setView:point styleBackgroundColor:pointNortalColor cornerRadius:widthAndHeigth/2 LayerWidth:0 Color:nil];
        [point setTag:i+1000];
        if(i == 0){
            [LJTool setView:point styleBackgroundColor:pointSelectColor cornerRadius:widthAndHeigth/2 LayerWidth:0 Color:nil];
        }
        [self addSubview:point];

    }
}

-(id) setPoitWidthAndHeigth:(int) WidthHeigth withPointBottomMargin:(int) marginToBottom widthPointMargin:(int) marginMax {
    pageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    // 是否支持滑动最顶端
    //    scrollView.scrollsToTop = NO;
    pageScrollView.delegate = self;
    // 设置内容大小
    //    pageScrollView.contentSize = CGSizeMake(320, 460*10);
    // 是否反弹
        pageScrollView.bounces = NO;
    // 是否分页
    pageScrollView.pagingEnabled = YES;
    // 是否滚动
    pageScrollView.scrollEnabled = true;
    //    pageScrollView.showsHorizontalScrollIndicator = NO;
    // 设置indicator风格
    pageScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    // 设置内容的边缘和Indicators边缘
    //    pageScrollView.contentInset = UIEdgeInsetsMake(0, 50, 50, 0);
    //    pageScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 50, 0, 0);
    // 提示用户,Indicators flash
    widthAndHeigth = WidthHeigth;
    marginBottom = marginToBottom;
    marginMaxWidth = marginMax;
    [self addSubview:pageScrollView];
    return self;
}


-(void) setAutoScroller:(Boolean )flag TimeInterval:(float) interval{
    AutoScroller = flag;
    timeInterval =interval;
    if(AutoScroller){
    //启动轮播定时器,间隔3秒
        aotuScrollTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(autoScroller:) userInfo:nil repeats:YES];
    }
}

-(void) autoScroller:(NSTimer *)timer{
        CGFloat imageWidth = pageScrollView.frame.size.width;
        CGFloat currentOffsetX = pageScrollView.contentOffset.x;
        if(currentOffsetX == imageWidth*(pointCount-1)){
            [pageScrollView  setContentOffset:CGPointMake(0, 0) animated:YES];
        }else{
            [pageScrollView  setContentOffset:CGPointMake(currentOffsetX+imageWidth, 0) animated:YES];
        }

}

// scrollView 已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int position = (scrollView.contentOffset.x + self.frame.size.width/2)/ self.frame.size.width;

    UIView *view1 = [self viewWithTag:1000+position];
    [view1 setBackgroundColor:pointSelectColor];   //判断那个点高亮状态

    if(position > 0){
        UIView *view2 = [self viewWithTag:1000+position-1];
        if (view2) {
            [view2 setBackgroundColor:pointNortalColor];
        }
    }
    if(position < pointCount){
        UIView *view3 = [self viewWithTag:1000+position+1];
        if (view3) {
            [view3 setBackgroundColor:pointNortalColor];
        }
    }
}

- (void) scrollViewWillBeginDragging: (UIScrollView*) scrollview{
    //当用户准备拖曳时停止定时器,以后重启需重新启动,不会自动启动
    if(aotuScrollTimer){
        [aotuScrollTimer invalidate];
    }
}

- (void) scrollViewDidEndDragging: (UIScrollView*) scrollview{
    //当用户停止拖曳时,定时器重启,自动轮播开始
    if(AutoScroller){
        //启动轮播定时器,间隔3秒
        aotuScrollTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(autoScroller:) userInfo:nil repeats:YES];
    }
}

@end

其他

+(void) setView:(UIView *)view styleBackgroundColor:(UIColor *)backgroundColor cornerRadius:(CGFloat) radius LayerWidth:(CGFloat) width Color:(UIColor *)borderColor{
    if (radius > 0) {
        view.layer.cornerRadius = radius;
        view.layer.masksToBounds = YES;
    }
    if (width > 0) {
        view.layer.borderWidth = width;
    }
    if (borderColor) {
        view.layer.borderColor = borderColor.CGColor;
    }
    if (backgroundColor) {
        [view setBackgroundColor:backgroundColor];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值