UIScrollView 实现比例缩放
2015-07-14 21:27
513 查看
#import "RootViewController.h" @interface RootViewController ()<UIScrollViewDelegate> { UIImageView *imageView; UILabel *scaleRatioLabel;// 显示倍率用的Label } @property (nonatomic, strong)UIScrollView *scrollView; @end @implementation RootViewController - (void)dealloc { self.scrollView = nil; } - (void)viewDidLoad { [super viewDidLoad]; self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.scrollView.delegate = self; [self.view addSubview:self.scrollView]; imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1-3.jpg"]]; [imageView setCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2.0, [UIScreen mainScreen].bounds.size.height/2.0)]; [self.scrollView addSubview:imageView]; //内容大小与图片大小一致 self.scrollView.contentSize = imageView.frame.size; // 最小缩放比例 self.scrollView.minimumZoomScale = 0.2f; // 最大缩放比例 self.scrollView.maximumZoomScale = 5.0f; // 用来显示倍率的Label scaleRatioLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2.0 - 25, [UIScreen mainScreen].bounds.size.height/2.0 - 12.5, 50, 25)]; [scaleRatioLabel setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:scaleRatioLabel]; } #pragma mark - UIScrollViewDelegate // 设置要缩放的控件 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return imageView; } // 处理结束缩放事件 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { [self.view bringSubviewToFront:scaleRatioLabel]; [scaleRatioLabel setAlpha:0.6f]; [scaleRatioLabel setBackgroundColor:[UIColor lightGrayColor]]; scaleRatioLabel.text = [NSString stringWithFormat:@" x%.1f",scale]; [UIView transitionWithView:scaleRatioLabel duration:2.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{ scaleRatioLabel.alpha = 0.0f; } completion:nil]; } @end
相关文章推荐
- UiAutomator喷射事件的源代码分析
- UIKit之浅析UIButton
- 关于继承UITableViewController若干问题
- Table的分割线偏移量设置 及其 UIEdgeInset详解
- iOS开发UI篇—UITabBarController简单介绍
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)
- NSArray, NSNumber, NSValue, NSSet, 数组排序
- UI基础(二)-图层
- IOS开发UI篇--一个侧滑菜单SlidingMenu
- UI_UItableView_AutoCell(自定义cell 高度)
- UI中一些名词的缩写
- 暑假选拔赛02 ---- Ysequence
- The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder i
- UI基础(一)
- String, StringBuilder , StringBuffer
- NGUI UIWrapContent小用法
- UITableView简单性能优化
- @DynamicUpdate(true) @DynamicInsert(true)使用的环境
- 算法导论 第七章:快速排序(Quicksort)
- Leetcode-N-Queens II