简易图片浏览器【可缩放图片,滑动后恢复正常】
来源:互联网 发布:大数据提供商 编辑:程序博客网 时间:2024/04/29 23:49
AppDelegate.h
#import "AppDelegate.h"#import "RootViewController.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]; [self.window makeKeyAndVisible]; RootViewController *rootCtrl = [[RootViewController alloc] init]; UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:rootCtrl]; NSMutableArray *mutArrary = [[NSMutableArray alloc] init]; for (int i=0; i<5; i++) { NSString *imgName = [NSString stringWithFormat:@"%d.JPG",i]; UIImage *image = [UIImage imageNamed:imgName]; [mutArrary addObject:image]; } rootCtrl.images = mutArrary; self.window.rootViewController = navCtrl; return YES;}@endRootViewController.h
@interface RootViewController : UIViewController<UIScrollViewDelegate>{ NSInteger _index;}@property(nonatomic, retain)NSArray *images; //存放显示的图片
RootViewController.m
#import "RootViewController.h"#import "PhotoScrollView.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; //创建滚动视图 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, 480)]; //隐藏水平滚动条 scrollView.showsHorizontalScrollIndicator = NO; scrollView.backgroundColor = [UIColor blackColor]; scrollView.delegate = self; //设置分页效果 scrollView.pagingEnabled = YES; //设置内容尺寸 scrollView.contentSize = CGSizeMake(340*_images.count, 480); [self.view addSubview:scrollView]; for (int i=0; i<self.images.count; i++) { PhotoScrollView *photoView = [[PhotoScrollView alloc] initWithFrame:CGRectMake(340*i, 0, 320, 480)]; photoView.tag = i + 100; //传值 photoView.image =_images[i]; [scrollView addSubview:photoView]; } }#pragma mark - UIScrollView delegate- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { //1.获取当前的页数 int currentPage = scrollView.contentOffset.x/340; //2.还原以前的缩放试图 if (currentPage != _index) { //取得视图 int tag = _index + 100; PhotoScrollView *view = (PhotoScrollView *)[scrollView viewWithTag:tag]; //还原 [view setZoomScale:1]; } //3.记录当前的页数 _index = currentPage;}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
PhotoScrollView.h
@interface PhotoScrollView : UIScrollView<UIScrollViewDelegate>{ UIImageView *_imageView;}@property(nonatomic, retain)UIImage *image;
PhotoScrollView.m
#import "PhotoScrollView.h"@implementation PhotoScrollView- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { _imageView = [[UIImageView alloc] initWithFrame:self.bounds]; //数据不能在这里设置// _imageView.image = _image; [self addSubview:_imageView]; //设置最大放大倍数 self.maximumZoomScale = 2.0; //设置最小缩小倍数 self.minimumZoomScale = .5; //隐藏滚动条 self.showsHorizontalScrollIndicator = NO; self.showsVerticalScrollIndicator = NO; //设置代理 self.delegate = self; //添加手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActoin:)]; tap.numberOfTapsRequired = 2; [self addGestureRecognizer:tap]; } return self;}//手势响应事件- (void)tapActoin:(UITapGestureRecognizer *)tap { if (self.zoomScale > 1) { //缩小 [self setZoomScale:1 animated:YES]; }else { //放大 [self setZoomScale:2 animated:YES]; } }- (void)setImage:(UIImage *)image { _image = image; _imageView.image = _image;}#pragma mark - UIScrollView delegate- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return _imageView;}@end
0 0
- 简易图片浏览器【可缩放图片,滑动后恢复正常】
- jquery 中在浏览器大小大于图片大小后图片正常显示,否则图片进行缩放
- 缩放图片(简易)
- 滑动条缩放图片
- 滑动条缩放图片
- ViewPager 可左右滑动和缩放的图片浏览
- 查看图片列表,选中之后查看大图,可缩放滑动
- Glide网络图片滑动查看,可缩放(PhotoView example)
- iOS开发(OC)——仿QQ微信的图片浏览器(缩放,滑动后还原,本地图片和网络图片都可以用)
- Glide加载图片变形,刷新后恢复正常
- Android 网络加载图片点击大图后 浏览 可 缩放
- 简易图片浏览器!
- 简易图片浏览器
- IOS 简易图片浏览器
- 通过左右滑动缩放图片
- extjs4 图片查看可缩放
- viewPager+photoView实现图片轮播和手势缩放功能,并且实现滑动到下一页后上一页缩放的图片会恢复到之前的默认状态(下)
- 图片居中自适应浏览器缩放
- 黑马程序员——高新技术—代理类与类加载器
- UVa 639 - Don't Get Rooked
- 树状数组-HDU1166
- node express
- 基本绘图
- 简易图片浏览器【可缩放图片,滑动后恢复正常】
- linux 下 JIRA6.3.8安装、配置、汉化
- 软件架构师应具备的十大特点
- u-boot-2014.10移植第9天----深入分析代码(四)
- 四种方案解决ScrollView嵌套ListView问题
- android Timer与TimerTask的相关操作
- android4.4 编译
- 阿里2014笔试题及答案
- zoj2107 最近点对问题 分治思想