博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios 自制放大镜效果demo
阅读量:5820 次
发布时间:2019-06-18

本文共 2994 字,大约阅读时间需要 9 分钟。

hot3.png

MagnifierView.h#import 
@interface MagnifierView : UIView {// CGPoint touchPoint;}@property (nonatomic, strong) UIView *viewToMagnify;@property (nonatomic, assign) CGPoint touchPoint;- (void)drawRect:(CGRect)rect;@endMagnifierView.m#import "MagnifierView.h"@implementation MagnifierView- (void)setTouchPoint:(CGPoint)pt { _touchPoint = pt; self.center = CGPointMake(pt.x, pt.y-50);//跟随touchmove 不断得到中心点}- (void)drawRect:(CGRect)rect { //绘制放大镜效果部分 CGContextRef context = UIGraphicsGetCurrentContext();//获取的是当前view的图形上下文 CGContextTranslateCTM(context,1*(self.frame.size.width*0.5),1*(self.frame.size.height*0.5 + 50));//重新设置坐标系原点 CGContextScaleCTM(context, 1.5, 1.5);//通过调用CGContextScaleCTM函数来指定x, y缩放因子 这里我们是扩大1.5倍 CGContextTranslateCTM(context,-1*(_touchPoint.x),-1*(_touchPoint.y)); [self.viewToMagnify.layer renderInContext:context];//直接在一个 Core Graphics 上下文中绘制放大后的图像,实现放大镜效果}@end///MagnifierView的使用#import "ViewController.h"#import "MagnifierView.h"@interface ViewController () { MagnifierView *loop;}@property (nonatomic, strong) NSTimer *touchTimer;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent *)event { //计时器,手指点中0.5秒后启动放大镜效果 self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addLoop) userInfo:nil repeats:NO]; if(loop == nil){ loop = [[MagnifierView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; loop.viewToMagnify = self.view; loop.layer.borderColor = [UIColor grayColor].CGColor; loop.layer.borderWidth = 2; loop.layer.cornerRadius = 50; loop.layer.masksToBounds = YES; } UITouch *touch = [touches anyObject]; loop.touchPoint = [touch locationInView:self.view]; [loop setNeedsDisplay]; [self.view addSubview:loop];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //将手指移动信息传出给 handleAction [self handleAction:touches];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //手指抬起,将loop(放大镜)去掉 [self.touchTimer invalidate]; self.touchTimer = nil; [loop removeFromSuperview]; loop = nil;}- (void)addLoop { [loop bringSubviewToFront:self.view];//让放大镜显示在最上层}- (void)handleAction:(id)timerObj { NSSet *touches = timerObj; UITouch *touch = [touches anyObject]; loop.touchPoint = [touch locationInView:self.view];//将本身的touch信息传递给放大镜,设置放大镜的中心点 [loop setNeedsDisplay];// loop drawRect:<#(CGRect)#>}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

 

转载于:https://my.oschina.net/zsyzone/blog/850186

你可能感兴趣的文章
.NET 设计规范--.NET约定、惯用法与模式-2.框架设计基础
查看>>
win7 64位+Oracle 11g 64位下使用 PL/SQL Developer 的解决办法
查看>>
BZOJ1997:[HNOI2010]PLANAR——题解
查看>>
BZOJ1014:[JSOI2008]火星人prefix——题解
查看>>
使用Unity3D引擎开发赛车游戏
查看>>
HTML5新手入门指南
查看>>
opennebula 开发记录
查看>>
ubuntu 修改hostname
查看>>
sql 内联,左联,右联,全联
查看>>
C++关于字符串的处理
查看>>
6、Web Service-拦截器
查看>>
Flask 源码流程,上下文管理
查看>>
stream classdesc serialVersionUID = -7218828885279815404, local class serialVersionUID = 1.
查看>>
ZAB与Paxos算法的联系与区别
查看>>
java 读取本地的json文件
查看>>
Breaking parallel loops in .NET C# using the Stop method z
查看>>
Android Content Provider Guides
查看>>
修改故障转移群集心跳时间
查看>>
[轉]redis;mongodb;memcache三者的性能比較
查看>>
微软职位内部推荐-Sr DEV
查看>>