ios 自制放大镜效果demo

原创文章,转载请注明出处: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:self
												selector:@selector(addLoop)
											   userInfo:nil
												repeats: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





  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值