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

 西瓜凉皮大夏天 发布于 2022-11-02 02:45

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


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

@interface AdScrollView ()
@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,没显示图片,求解

2 个回答
  • 不会。

    title

    123123

    2022-11-03 16:01 回答
  • make.width.equalTo(self.contenView.width);
    

    替换成

    make.width.equalTo(self.scrollView.width);
    
    2022-11-03 16:09 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有