UIScrollView和UIPageControl的配合使用 - iOS - UI基础知识总结11
2015-06-09 20:06
633 查看
注意:MRC模式
在.h中声明两个属性
释放
在viewDidLoad中创建UIScrollView对象和UIPageControl对象
添加一个宏定义#define kPageControlHeight 50
在.h中声明两个属性
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @property (nonatomic, retain) UIScrollView *myScrollView; @property (nonatomic, retain) UIPageControl *myPageControl; @end
释放
- (void)dealloc { [_myScrollView release]; [_myPageControl release]; [super dealloc]; }
在viewDidLoad中创建UIScrollView对象和UIPageControl对象
添加一个宏定义#define kPageControlHeight 50
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; self.myScrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; self.myScrollView.backgroundColor = [UIColor grayColor]; [self.view addSubview:self.myScrollView]; [self.myScrollView release]; // 给scrollView加图片 for (int i = 0; i < 6; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)]; imageView.image = image; [self.myScrollView addSubview:imageView]; [imageView release]; } // 给scrollView设置contentSize self.myScrollView.contentSize = CGSizeMake(6*self.view.frame.size.width, self.view.frame.size.height); // 整页翻动(一页一页翻动) self.myScrollView.pagingEnabled = YES; // 给scrollView添加代理对象 self.myScrollView.delegate = self; // 创建UIPageControl self.myPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - kPageControlHeight, self.view.frame.size.width, kPageControlHeight)]; // 设置页数 self.myPageControl.numberOfPages = 6; // 设置当前选中的点的颜色 self.myPageControl.currentPageIndicatorTintColor = [UIColor redColor]; // 设置未被选中的点的颜色 self.myPageControl.pageIndicatorTintColor = [UIColor cyanColor]; [self.view addSubview:self.myPageControl]; [self.myPageControl release]; // 给pageControl设置点击事件 [self.myPageControl addTarget:self action:@selector(myPageControlAction:) forControlEvents:UIControlEventValueChanged]; }
#pragma mark - UIScrollView Delegate // scrollView的代理方法 // 滑动减速完成 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"减速完成"); // 滑动一次结束时让pageControl的点跳动 // 先找到偏移量,再根据偏移量设置currentPage self.myPageControl.currentPage = self.myScrollView.contentOffset.x / self.view.frame.size.width; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@"正在滑动"); } - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { NSLog(@"开始减速"); } - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { NSLog(@"将要结束拖拽"); } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { NSLog(@"已经结束拖拽"); } - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { NSLog(@"已经完成缩放"); }
#pragma mark - pageControl 点击事件处理 - (void)myPageControlAction:(UIPageControl *)sender { self.myScrollView.contentOffset = CGPointMake(sender.currentPage * self.view.frame.size.width, 0); }
相关文章推荐
- No unique bean of type
- POJ3080:Blue Jeans(后缀数组)
- easyUI datagrid ajax实时刷新 动态添加列名
- UIView的drawRect和layoutSubviews方法
- UIScrollView的基本使用方法和注意事项 - iOS - UI基础知识总结10
- NSURLRequestCachePolicy 缓存策略
- apue.h: No such file or directory
- android&Bluetooth
- easyui textarea IE8中无法换行
- PyQt + QML 快速开发GUI入门总结
- UIDynamics -- 力学部分
- SPH fluid simulation methods & source codes (cpu & gpu)
- 类似微信的ChattingUi
- N-Queens(N皇后问题) --Java版
- UITableView创建样式
- Distinct Subsequences leetcode 115
- Subsequence - HDU 3530 单调队列优化
- 关于UIImagePickerController的理解 (区别于上文的AVCapture)
- 关于ExecuteNonQuery()执行成功却返回-1的问题
- iOS中UIActionSheet的使用