iOS 坐标转换,tableViewCell图片点击放大缩小回原位 —— HERO博客
2016-03-17 00:56
567 查看
之前写了一片文章:基于MVC设计模式练习UITableView使用,今天就根据这篇文章中的例子简述坐标转换实际应用。
首先,了解一下坐标转换的几个方法:
//返回CGRect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
例:将C视图中B的坐标位置转换为在A中的坐标位置,两个方法一样的结果
CGRect frame = [C convertRect:B toView:A];
CGRect frame = [A convertRect:B fromView:C];
//返回CGPoint,对象返回值不同,用法和上面相同
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
改了一下之前的代码,使自定义的tableViewCell中的图片点击放大,再次点击图片缩小回到原位,首先看一下效果图:
下面贴上自定义的Cell代码,其他代码可以点击上面的文章连接查看:
首先,了解一下坐标转换的几个方法:
//返回CGRect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
例:将C视图中B的坐标位置转换为在A中的坐标位置,两个方法一样的结果
CGRect frame = [C convertRect:B toView:A];
CGRect frame = [A convertRect:B fromView:C];
//返回CGPoint,对象返回值不同,用法和上面相同
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
改了一下之前的代码,使自定义的tableViewCell中的图片点击放大,再次点击图片缩小回到原位,首先看一下效果图:
下面贴上自定义的Cell代码,其他代码可以点击上面的文章连接查看:
#import <UIKit/UIKit.h> @class HWCellModel; @interface HWTableViewCell : UITableViewCell @property (nonatomic, weak) UILabel *lable; @property (nonatomic, strong) HWCellModel *model; + (instancetype)cellWIthTableView:(UITableView *)tableView; @end #import "HWTableViewCell.h" #import "HWCellModel.h" @interface HWTableViewCell () @property (nonatomic, weak) UIImageView *imgView; @property (nonatomic, weak) UIImageView *imagView; @property (nonatomic, weak) UILabel *subLabel; @property (nonatomic, assign) CGRect lastFrame; @end @implementation HWTableViewCell + (instancetype)cellWIthTableView:(UITableView *)tableView { //cell复用,唯一标识 static NSString *identifier = @"HWCell"; //先在缓存池中取 HWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; //缓存池中没有再创建,并添加标识,cell移出屏幕时放入缓存池以复用 if (cell == nil) { cell = [[HWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } return cell; } //重写init方法构建cell内容 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { //图片 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 80, 60)]; [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]]; imageView.userInteractionEnabled = YES; [self.contentView addSubview:imageView]; self.imgView = imageView; //标题 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 15, 200, 20)]; label.font = [UIFont systemFontOfSize:20.0f]; [self.contentView addSubview:label]; self.lable = label; //副标题 UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 40, 200, 13)]; subLabel.font = [UIFont systemFontOfSize:13.0f]; [self.contentView addSubview:subLabel]; self.subLabel = subLabel; self.backgroundColor = [UIColor whiteColor]; } return self; } //重写set方法,模型传递 - (void)setModel:(HWCellModel *)model { _model = model; self.imgView.image = [UIImage imageNamed:model.image]; self.lable.text = model.title; self.subLabel.text = model.subTitle; } - (void)tapImageView:(UITapGestureRecognizer *)recognizer { //添加遮盖 UIView *cover = [[UIView alloc] init]; cover.frame = [UIScreen mainScreen].bounds; cover.backgroundColor = [UIColor clearColor]; [cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCover:)]]; [[UIApplication sharedApplication].keyWindow addSubview:cover]; //添加图片到遮盖上 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hero"]]; imageView.frame = [cover convertRect:recognizer.view.frame fromView:self]; self.lastFrame = imageView.frame; [cover addSubview:imageView]; self.imagView = imageView; //放大 [UIView animateWithDuration:0.3f animations:^{ cover.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8]; CGRect frame = imageView.frame; frame.size.width = cover.frame.size.width; frame.size.height = cover.frame.size.width * (imageView.image.size.height / imageView.image.size.width); frame.origin.x = 0; frame.origin.y = (cover.frame.size.height - frame.size.height) * 0.5; imageView.frame = frame; }]; } - (void)tapCover:(UITapGestureRecognizer *)recognizer { [UIView animateWithDuration:0.3f animations:^{ recognizer.view.backgroundColor = [UIColor clearColor]; self.imagView.frame = self.lastFrame; }completion:^(BOOL finished) { [recognizer.view removeFromSuperview]; self.imagView = nil; }]; } @end
相关文章推荐
- java经纬度转大地坐标
- 直升机飞行控制
- 经纬度坐标转换为屏幕坐标
- GIS坐标转换——理论基础(二)
- 对单个Geometry使用四参数转换
- 自定义UITableViewCell 上的多个按钮点击事件处理
- 地图中坐标转换的那些事儿
- iOS 百度地图之坐标转换
- UIView中的坐标转换
- cocos2d-x 精灵相对屏幕的坐标
- 腾讯坐标转换百度坐标
- 关于百度坐标转火星坐标的问题
- iOS 基于MVC设计模式练习UITableView使用 —— 4000 HERO博客
- iOS 基于MVVM设计模式练习UITableView使用 —— HERO博客
- Unity3D中世界坐标转换到NGUI坐标
- WGS84 GCJ02和BD09坐标系相互转换代码
- 计算View中的子View在View的superview中的坐标
- 【Django基础入门】空间数据库查询
- bingMap的使用小结
- 自制45度2D引擎之坐标转换