UIScrollView和UIPageController
来源:互联网 发布:大闸蟹 二恶英 知乎 编辑:程序博客网 时间:2024/05/21 08:59
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; //创建导航栏控制器 UINavigationController* nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]]; //设置应用程序的根控制器 self.window.rootViewController = nav; return YES;}
#import "ViewController.h"#define Width [UIScreen mainScreen].bounds.size.width#define Heigth [UIScreen mainScreen].bounds.size.heigth@interface ViewController ()<UIScrollViewDelegate>@property (retain)UIScrollView* scrollView;@property (retain)UIPageControl* pageControl;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake((Width-320)/2, 65, 320, 480)]; self.scrollView.backgroundColor = [UIColor cyanColor]; [self.view addSubview:self.scrollView]; //要设置代理⚠️ self.scrollView.delegate=self; for (NSInteger i = 0; i < 4; i++) { UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 480)]; [self.scrollView addSubview:imageView]; imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"美女0%ld.jpg",i+1]]; //防止内容视图下滑 self.automaticallyAdjustsScrollViewInsets = NO; //设置滚动范围 self.scrollView.contentSize = CGSizeMake(320*4, 480); //取消反弹效果 self.scrollView.bounces = NO; //隐藏水平方向的滑块 self.scrollView.showsHorizontalScrollIndicator = NO; //设置分页显示 self.scrollView.pagingEnabled = YES; //创建UIPaneControl对象 self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((Width-100)/2, 450+64, 100, 30)]; self.pageControl.backgroundColor = [UIColor clearColor]; //设置总页数 self.pageControl.numberOfPages = 4; //设置当前页 self.pageControl.currentPage = 0; //让self.pageControl绑定事件 [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:self.pageControl]; } //左边按钮 UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; leftBtn.frame = CGRectMake(0, 0, 60, 30); leftBtn.backgroundColor = [UIColor grayColor]; [leftBtn setTitle:@"上一页" forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn]; //右边按钮 UIButton* rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; rightBtn.frame = CGRectMake(0, 0, 60, 30); rightBtn.backgroundColor = [UIColor grayColor]; [rightBtn setTitle:@"下一页" forState:UIControlStateNormal]; [rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];}-(void)leftAction:(UIBarButtonItem*)sender{ NSLog(@"上一页被点!!!!"); NSInteger page = self.pageControl.currentPage; if (page > 0) { page--; [UIView animateWithDuration:0.5 animations:^{ self.pageControl.currentPage = page; self.scrollView.contentOffset = CGPointMake(320*page, 0); }]; }}-(void)rightAction:(UIBarButtonItem*)sender{ NSLog(@"下一页被点!!!!"); NSInteger page = self.pageControl.currentPage; if (page < 3) { page++; [UIView animateWithDuration:0.5 animations:^{ self.pageControl.currentPage = page; self.scrollView.contentOffset = CGPointMake(320*page, 0); }]; }}-(void)pageAction:(UIPageControl*)page{ NSLog(@"%ld",(long)page.currentPage); [UIView animateWithDuration:0.5 animations:^{ self.scrollView.contentOffset = CGPointMake(page.currentPage*320, 0); }];}#pragma mark-UIScrollViewDelegate-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"hehe"); //获取偏移量 CGPoint point = scrollView.contentOffset; NSLog(@"%@",NSStringFromCGPoint(point)); //计算出当前展示的是第几张照片 NSInteger page = point.x/300; self.pageControl.currentPage = page;}
阅读全文
0 0
- 28.UIScrollView和UIPageController
- UIScrollView和UIPageController
- UIPageController和UIScrollView结合使用
- iOS开发 UI UIScrollView和UIPageController
- IOS控件的应用UIPageController与UIScrollView
- IOS UIScrollView与UIPageController配合使用
- UIPageController与UIScrollView的联合使用
- UIPageController与UIScrollView的联合使用
- 3、iOS 开发之 UIScrollView UIPageController NSTimer
- UIScrollView、UITableView与UIPageController的混合用法
- UIPageController
- UIPageController
- UIPagecontroller
- IOS UIPageController
- IOS UIPageController
- IOS UIPageController
- 转载 -uipagecontroller
- 自定义UIPageController
- 51Nod 1084 矩阵取数问题
- mmc子系统总结
- 限制容器对CPU的使用
- NOIP2005普及组第四题
- js的打印(输出)方式console.log(),console.dir(),console.table()
- UIScrollView和UIPageController
- Android简单物流查询
- 锁Lock
- HTTP状态码大全
- 点对点通信、数据链路层、点对点链路、PPP、PPPOE、PPTP、L2TP-VPN简介
- 设计模式(Design Pattern)
- Android 7.0中数据流量开关与数据图标对应
- virtualbox 虚拟机静态IP设置(用于Host Only和NAT共存的方式)
- LeetCode 41.First Missing Positive