iOS图片轮播器 - Go语言中文社区

iOS图片轮播器


第一次写博客。。。
小小的试验一下。。。

图片轮播器。
代码记录:

ViewController.m 文件中(局部):

#import "SONGTgHeaderView.h" // 添加图片轮播
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 90;

    // 设置footerView
    SONGTgFooterView *footer = [SONGTgFooterView footerView]; // -----7
//    footer.controller = self; // -----8
    footer.delegate = self;
    self.tableView.tableFooterView = footer;

    //设置headerView (添加图片轮播)
    self.tableView.tableHeaderView = [SONGTgHeaderView SONGTgHeaderView];
}

SONGTgHeaderView.h 文件下的代码

#import <UIKit/UIKit.h>

@interface SONGTgHeaderView : UIView
+(instancetype)SONGTgHeaderView;
@end

SONGTgHeaderView.m 文件下的代码

#import "SONGTgHeaderView.h"
#define imageCount 5

@interface SONGTgHeaderView() <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView; // 拖线
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl; // 拖线
@property (nonatomic,strong) NSTimer *timer;

@end

@implementation SONGTgHeaderView

+(instancetype)SONGTgHeaderView {
    return [[[NSBundle mainBundle] loadNibNamed:@"SONGTgHeaderView" owner:nil options:nil] lastObject];
}

// 当一个对象从xib中创建初始化完毕的时候就会调用一次
-(void)awakeFromNib {
#pragma mark 图片可以滚动
    CGFloat imgY = 0;
    CGFloat imgW = self.scrollView.frame.size.width;
    CGFloat imgH = self.scrollView.frame.size.height;

    for (int i = 0; i<imageCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        CGFloat imgX = i * imgW;
        imageView.frame = CGRectMake(imgX, imgY, imgW, imgH);
        NSString *name = [NSString stringWithFormat:@"ad_0%d",i];
        imageView.image = [UIImage imageNamed:name];

        [self.scrollView addSubview:imageView];
    }

    // 滚动范围
    self.scrollView.contentSize = CGSizeMake(imageCount * imgW, 0);
    // 在拖拽到一半时,自动分页
    self.scrollView.pagingEnabled = YES;
    // 设置pageControl页数
    self.pageControl.numberOfPages = imageCount;
    // 添加定时器
    [self addTimer];
}

// 添加定时器
-(void)addTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(autoplay) userInfo:nil repeats:YES];
    // 优先级:
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

// 暂停定时器
-(void)removeTimer {
    [self.timer invalidate]; // invalidate:使无效
    self.timer = nil;
}

#pragma mark - 转换图片方法
// 自动播放
-(void)autoplay {
    NSInteger page;
    if (self.pageControl.currentPage == imageCount - 1) { // 如果是最后一张图片
        page = 0; // 自动轮播到第一张图片
    }else {
        page = self.pageControl.currentPage + 1; // 或者,轮播下一张图片
    }

    // scrollView滚动位置
    CGFloat offsetX = page * self.scrollView.frame.size.width;
    CGPoint offset = CGPointMake(offsetX, 0); // 水平方向滚动,X是当前页数乘以宽度,Y是0。
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark - 代理方法
// 将pagecontrol 与图片捆绑 (显示分页点)
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scrollW = scrollView.frame.size.width;
    int page = (scrollW *0.5 + scrollView.contentOffset.x) / scrollW;
    self.pageControl.currentPage = page;
}

// 开始拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
}

// 结束拖拽
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self addTimer];
}

@end

xib:
Page Control 不能在Scroll View 内部

效果图:
无视下方tableview = =

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_36174620/article/details/53044830
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 16:10:34
  • 阅读 ( 1522 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢