ios 自制放大镜效果demo

来源:互联网 发布:海关数据免费查询 编辑:程序博客网 时间:2024/05/01 10:59

原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9334055

demo功能:实现图片和界面的放大镜功能,在整个界面上移动手指,放大部分也随之移动。iphone 6.1测试通过。

demo说明: MagnifierView里是绘制放大镜的主要代码。TouchReader.m中是主要界面。TouchReader在touchmove方法中将手指滑动信息传递给MagnifierView

demo截屏:


demo主要代码:

MagnifierView.m

- (void)setTouchPoint:(CGPoint)pt {touchPoint = pt;self.center = CGPointMake(pt.x, pt.y-10);//跟随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));//重新设置坐标系原点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 上下文中绘制放大后的图像,实现放大镜效果}

TouchReader.m


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {self.clipsToBounds = NO;    //计时器,手指点中0.5秒后启动放大镜效果self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:selfselector:@selector(addLoop)   userInfo:nilrepeats:NO];// just create one loop and re-use it.if(loop == nil){loop = [[MagnifierView alloc] init];loop.viewToMagnify = self;}UITouch *touch = [touches anyObject];loop.touchPoint = [touch locationInView:self];[loop setNeedsDisplay];}- (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 release];loop = nil;}- (void)addLoop {[loop makeKeyAndVisible];//让放大镜显示在最上层}- (void)handleAction:(id)timerObj {NSSet *touches = timerObj;UITouch *touch = [touches anyObject];loop.touchPoint = [touch locationInView:self];//将本身的touch信息传递给放大镜,设置放大镜的中心点[loop setNeedsDisplay];}

demo下载地址:http://download.csdn.net/detail/donny_zhang/5758041





原创粉丝点击