UIScrollView和UIPageControl组合照片浏览滑动效果
2015-10-03 23:02
465 查看
转自:http://blog.csdn.net/conslee/article/details/19072309
如有侵犯,请来信oiken@qq.com
![](http://img.blog.csdn.net/20140211112802250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29uc2xlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
一个朋友去面试,让写一个用UIScrollView和UIPageControl,一激动说需要半个小时,没时间写了,浪费一次面试机会。
会写的几分钟就可以搞定了,方法其实有很多,小弟在这里献丑了,各位大牛莫要嘲笑。
刚好在学习gcd,就用gcd简单实现一下了。有错误的地方各位指点指点:
直接在入口类里面实现了,
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UIScrollViewDelegate>
{
NSMutableArray *_mutArrImg;
UIPageControl *_pageControl;
UIScrollView *_scrollView;
}
@property (strong, nonatomic) UIWindow *window;
@end
分别定义NSMutableArray *_mutArrImg、UIPageControl *_pageControl、UIScrollView *_scrollView全局变量。
#import "AppDelegate.h"
@implementation AppDelegate
\\这个方法,我叫它入口方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
_scrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];
[self.window addSubview:_scrollView];
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(320*4, self.window.frame.size.height);
_scrollView.pagingEnabled = YES;
_mutArrImg = [NSMutableArray arrayWithCapacity:0];
for (int i=0; i<4; i++) {
NSURL *url = [NSURL URLWithString:@"http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imgData];
dispatch_sync(dispatch_get_main_queue(), ^{
[_mutArrImg addObject:img];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(320*i, 0, 320, self.window.frame.size.height)];
imgView.image = img;
[_scrollView addSubview:imgView];
});
});
}
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 30)];
_pageControl.numberOfPages = 4;
_pageControl.currentPage = 0;
_pageControl.pageIndicatorTintColor = [UIColor redColor];
[self.window addSubview:_pageControl];
[_pageControl addTarget:self action:@selector(actionCotrolPage:) forControlEvents:UIControlEventValueChanged];
[self.window makeKeyAndVisible];
return YES;
}
\\_pageControl绑定的方法,来控制_scrollView的偏移量的
- (IBAction)actionCotrolPage:(id)sender
{
[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage * 320, 0) animated:YES];
}
//UIScrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
_pageControl.currentPage = _scrollView.contentOffset.x/320;
[self actionCotrolPage:pageControl];//确保能准确落在位置上
}.
.
@end
如有侵犯,请来信oiken@qq.com
一个朋友去面试,让写一个用UIScrollView和UIPageControl,一激动说需要半个小时,没时间写了,浪费一次面试机会。
会写的几分钟就可以搞定了,方法其实有很多,小弟在这里献丑了,各位大牛莫要嘲笑。
刚好在学习gcd,就用gcd简单实现一下了。有错误的地方各位指点指点:
直接在入口类里面实现了,
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UIScrollViewDelegate>
{
NSMutableArray *_mutArrImg;
UIPageControl *_pageControl;
UIScrollView *_scrollView;
}
@property (strong, nonatomic) UIWindow *window;
@end
分别定义NSMutableArray *_mutArrImg、UIPageControl *_pageControl、UIScrollView *_scrollView全局变量。
#import "AppDelegate.h"
@implementation AppDelegate
\\这个方法,我叫它入口方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
_scrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];
[self.window addSubview:_scrollView];
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(320*4, self.window.frame.size.height);
_scrollView.pagingEnabled = YES;
_mutArrImg = [NSMutableArray arrayWithCapacity:0];
for (int i=0; i<4; i++) {
NSURL *url = [NSURL URLWithString:@"http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imgData];
dispatch_sync(dispatch_get_main_queue(), ^{
[_mutArrImg addObject:img];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(320*i, 0, 320, self.window.frame.size.height)];
imgView.image = img;
[_scrollView addSubview:imgView];
});
});
}
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 30)];
_pageControl.numberOfPages = 4;
_pageControl.currentPage = 0;
_pageControl.pageIndicatorTintColor = [UIColor redColor];
[self.window addSubview:_pageControl];
[_pageControl addTarget:self action:@selector(actionCotrolPage:) forControlEvents:UIControlEventValueChanged];
[self.window makeKeyAndVisible];
return YES;
}
\\_pageControl绑定的方法,来控制_scrollView的偏移量的
- (IBAction)actionCotrolPage:(id)sender
{
[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage * 320, 0) animated:YES];
}
//UIScrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
_pageControl.currentPage = _scrollView.contentOffset.x/320;
[self actionCotrolPage:pageControl];//确保能准确落在位置上
}.
.
@end
相关文章推荐
- 利用quietHDD解决硬盘C1增长以及异响问题
- LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
- 张氏web漏洞检查工具发布-英文名:Mr.Zhang Web Leak Scanner GUI
- 动态计算UITableViewCell高度详解
- hdu 1242 Rescue(bfs+优先队列)
- SPOJ QTREE - Query on a tree(树链剖分)
- iOS大典之UIScrollView
- [java学习笔记]java语言基础概述之转义字符&break&continue
- android--day02(UI、事件)
- iOS图文攻略之UILabel属性
- UITableViewDelegate方法
- 选择Key-Value Store
- Key-Value存储系统简介
- (一三〇)UITextField的光标操作扩展
- (一三〇)UITextField的光标操作扩展
- Permutation Sequence
- iOS图文攻略之UIView 属性
- Leetcode catalogue
- 关于queue和stack没有clear方法
- sqlite采用UUID主键和int型自增主键性能对比测试