轮播图

#import "ViewController.h"#import "SecViewController.h"@interface ViewController (){

//实现三个属性

UIScrollView *scroll;

NSArray *imgArr;//图片

UIPageControl *page;//页面

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//self.view.backgroundColor = [UIColor whiteColor];

scroll = [[UIScrollView alloc]initWithFrame:self.view.frame];

//添加协议

scroll.delegate = self;

[self.view addSubview:scroll];

imgArr = @[@"guide1",@"guide2",@"guide3",@"guide4",];

//设置图片进行滚动

scroll.pagingEnabled = YES;

for (int i = 0; i < imgArr.count; i++)

{

UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(scroll.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];

img.userInteractionEnabled = YES;

if (i == imgArr.count - 1)

{

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setTitle:@"立即体验" forState:UIControlStateNormal];

btn.frame = CGRectMake((self.view.frame.size.width -100) / 2 , 600 , 100, 44);

[btn addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];

btn.layer.cornerRadius = 10;

btn.layer.masksToBounds = YES;

btn.backgroundColor = [UIColor redColor];

[img addSubview:btn];

}

img.image = [UIImage imageNamed:imgArr[i]];

[scroll addSubview:img];

}

scroll.contentSize = CGSizeMake(self.view.frame.size.width * imgArr.count, self.view.frame.size.height);

//取消弹簧效果

scroll.bounces = NO;

//隐藏滚动条

scroll.showsHorizontalScrollIndicator = NO;

//页码  (点)

page = [[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width -100) / 2, 536, 100, 20)];

page.numberOfPages = imgArr.count;

//设置初始页码

page.currentPage = 0;

//设置页码的颜色

page.pageIndicatorTintColor = [UIColor yellowColor];

[self.view addSubview:page];

}

#pragma -

#pragma mark -UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

NSLog(@"contentOffset: x: %f,y: %f",scrollView.contentOffset.x,scrollView.contentOffset.y);

//求余

NSInteger index = scroll.contentOffset.x / scrollView.frame.size.width;

page.currentPage = index;

}

- (void)Click

{

SecViewController *mc = [[SecViewController alloc]init];

[self presentViewController:mc animated:YES completion:^{

}];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容