时间:2021-05-20
上代码前,我们先来看下实现的效果图:
WelcomeViewController.h
WelcomeViewController.m
#import "WelcomeViewController.h" #define IMAGECOUNT 3 @interface WelcomeViewController () <UIScrollViewDelegate> @property (nonatomic, strong)UIPageControl *pageControl; @end @implementation WelcomeViewController - (void)viewDidLoad { [super viewDidLoad]; //创建ScrollView UIScrollView *sv = [[UIScrollView alloc] init]; sv.frame = self.view.bounds; //设置边缘不弹跳 sv.bounces = NO; //整页滚动 sv.pagingEnabled = YES; sv.showsHorizontalScrollIndicator = NO; //加入多个子视图(ImageView) for(NSInteger i=0; i<IMAGECOUNT; i++){ NSString *imgName = [NSString stringWithFormat:@"%ld", i+1]; UIImage *image = [UIImage imageNamed:imgName]; UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; CGRect frame = CGRectZero; frame.origin.x = i * sv.frame.size.width; frame.size = sv.frame.size; imageView.frame = frame; [sv addSubview:imageView]; if(i==IMAGECOUNT-1){ //开启图片的用户点击功能 imageView.userInteractionEnabled = YES; //加个按钮 UIButton *button = [[UIButton alloc]init]; button.frame = CGRectMake((imageView.frame.size.width-150)/2, imageView.frame.size.height*0.8, 150, 40); button.backgroundColor = [UIColor orangeColor]; [button setTitle:@"立即体验" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont boldSystemFontOfSize:16]; [imageView addSubview:button]; [button addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside]; } } sv.contentSize = CGSizeMake(IMAGECOUNT * sv.frame.size.width, sv.frame.size.height); [self.view addSubview:sv]; //加入页面指示控件PageControl UIPageControl *pageControl = [[UIPageControl alloc]init]; self.pageControl = pageControl; //设置frame pageControl.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 20); //分页面的数量 pageControl.numberOfPages = IMAGECOUNT; //设置小圆点渲染颜色 pageControl.pageIndicatorTintColor = [UIColor whiteColor]; //设置当前选中小圆点的渲染颜色 pageControl.currentPageIndicatorTintColor = [UIColor redColor]; //关闭用户点击交互 pageControl.userInteractionEnabled = NO; [self.view addSubview:pageControl]; sv.delegate = self; } - (void)enter { NSLog(@"进入应用"); } //UIScrollViewDelegate方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint offset = scrollView.contentOffset; if(offset.x<=0){ offset.x = 0; scrollView.contentOffset = offset; } NSUInteger index = round(offset.x / scrollView.frame.size.width); self.pageControl.currentPage = index; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言在iOS11发布之后,出现了一系列适配相关的问题,UIScrollView在pagingEnabled=YES时滑动手势不灵敏,UITableView的滑动
本文介绍了iOS利用UIScrollView实现图片的缩放实例代码,分享给大家:第一步:添加scrollView到控制器中?1234UIScrollView*s
一、好多App都有上下滑动UIScrollview隐藏或者显示导航栏,在这里我说说我觉得有用的几种方法:1.iOS8之后系统有一个属性hidesBarsOnSw
前言最近因为工作项目中需要用到UIScrollView嵌套UItableView嵌套交互问题,顺便网上搜了下的demo,发现实现的效果并不是很理想,滑动偶尔会有
Android开发仿IOS滑动开关实现代码Android与iOS相比,ios好多控件都是自带的,而android需要使用自定义来实现。今天说的是ios的滑动开关