ios 自定义的scrollView,不显示图片 用Masonry约束的

发布于 2022-09-01 07:17:43 字数 5093 浏览 26 评论 0

RT,自定义的一个ScrollView不显示


#import <UIKit/UIKit.h> @protocol AdScrollViewDelegate <NSObject> /** * 点击了哪张图片 * */ -(void)adClick:(NSInteger)count; @end @interface AdScrollView : UIView @property(nonatomic,weak)id<AdScrollViewDelegate>delegate; /** * 创建一个滚动Ad * * @param count 图片的数量 * @param timeInterval 几秒滚动一张 * * @return self */ -(id)initWithImageCount:(NSInteger )count andTimeInterval:(NSInteger)timeInterval; /** * 设置是否开启自动滚动 * */ -(void)setAutoScroll:(BOOL)isScroll; @end
#import "AdScrollView.h"

@interface AdScrollView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scrollView;
@property(nonatomic,strong)UIView * contenView;
@property(nonatomic,strong)UIPageControl * page;
@property(nonatomic,strong)NSTimer * timer;
@property(nonatomic,assign)NSInteger * num;

@end

@implementation AdScrollView

-(id)initWithImageCount:(NSInteger )count andTimeInterval:(NSInteger)timeInterval
{
    self = [super init];
    if (self)
    {

    [self createScrollView];
    [self createPage:count];
    [self createContenView];
    [self loadImageNum:count];

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.scrollView addGestureRecognizer:tap];

    self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(autoShowNext:) userInfo:@(count) repeats:YES];
    self.num = 0;
    }

    return self;
}

-(void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor redColor];
    [self addSubview:self.scrollView];
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

-(void)createContenView
{
    self.contenView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contenView];
    self.contenView.backgroundColor = [UIColor redColor];
    [self.contenView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.height.equalTo(self.scrollView);
    }];
}
//图片已经预先改成ad0,ad1这种格式
-(void)loadImageNum:(NSInteger)count
{

    UIImageView *lastView = nil;
    for ( int i = 1 ; i < 3 ; i ++)
    {
        UIImageView * subv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"ad%d",i]]];
        [self.contenView addSubview:subv];


        [subv makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastView ? lastView.right : @0);
            make.bottom.equalTo(@0);
            make.width.equalTo(self.contenView.width);
            make.height.equalTo(self.contenView.width);
        }];

        lastView = subv;
    }
    [self.contenView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastView.right);
    }];
}

-(void)createPage:(NSInteger)count
{
    self.page = [[UIPageControl alloc] init];
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.page.currentPage = 0;
    self.page.numberOfPages = count;
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    [self addSubview:self.page];
    [self.page mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bottom);
        make.centerX.equalTo(self.centerX);
        make.size.mas_equalTo(CGSizeMake(self.bounds.size.width/2 , 30));
    }];

}

-(void)autoShowNext:(NSTimer * )sender
{

    NSInteger count = [sender.userInfo integerValue];

    [self.scrollView setContentOffset:CGPointMake(self.bounds.size.width*((int)(self.num)%count), 0) animated:YES];
    self.num++;

}

-(void)setAutoScroll:(BOOL)isScroll
{
    int num = isScroll?1:0;
    switch (num)
    {
        case YES:
        {
            [self.timer fire];
        }
            break;

        default:
        {
            [self.timer invalidate];
        }
            break;
    }
}
-(void)layoutSubviews
{
    [super layoutSubviews];
}
-(void)tap:(UITapGestureRecognizer * )tap
{
    [self.delegate adClick:self.page.currentPage];
}

- (void) scrollViewDidScroll:(UIScrollView *)sender {
    // 得到每页宽度
    CGFloat pageWidth = sender.frame.size.width;
    // 根据当前的x坐标和页宽度计算出当前页数
    int currentPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.page.currentPage = currentPage;
}

@end
- (void)viewDidLoad
{
    AdScrollView * ad = [[AdScrollView alloc] initWithImageCount:2 andTimeInterval:2];
    ad.delegate = self;
    [ad setAutoScroll:YES];
    [self.view addSubview:ad];
    [ad makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets
        (UIEdgeInsetsMake(64,0,self.view.bounds.size.height/3*2-50,0));
    }];
}

整个过程就是这样,但是只显示出scrollView,没显示图片,求解

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恬淡成诗 2022-09-08 07:17:43
make.width.equalTo(self.contenView.width);

替换成

make.width.equalTo(self.scrollView.width);
苍白女子 2022-09-08 07:17:43

不会。

title

123123

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文