ios 自定义的scrollView,不显示图片 用Masonry约束的
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
替换成
不会。
title
123123