本文实现了放大镜功能,具体用法如下
1、将MagnifierView类导入到工程中,在例子中就有的。
下载地址:http://pan.baidu.com/s/1eUz2Q http://download.csdn.net/detail/qqmcy/7020075
ViewController.h
#import <UIKit/UIKit.h>
#import "MagnifierView.h"
@interface ViewController : UIViewController
@property (strong , nonatomic) MagnifierView* loop;
@property (strong , nonatomic) NSTimer* touchTimer;
@end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"changmen.jpg"]];
image.frame = CGRectMake(0, 0, 320, 548);
[self.view addSubview:image];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(addLoop) userInfo:nil repeats:NO];
if (self.loop == nil) {
self.loop = [[MagnifierView alloc] init];
self.loop.viewToMagnify = self.view;
}
UITouch* touch = [touches anyObject];
self.loop.touchPoint = [touch locationInView:self.view];
[self.loop setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleAction:touches];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.touchTimer invalidate];
self.touchTimer = nil;
self.loop = nil;
}
- (void)handleAction:(id)timerObj {
NSSet *touches = timerObj;
UITouch *touch = [touches anyObject];
self.loop.touchPoint = [touch locationInView:self.view];
[self.loop setNeedsDisplay];
}
- (void)addLoop {
// add the loop to the superview. if we add it to the view it magnifies, it'll magnify itself!
//[self.superview addSubview:loop];
[self.loop makeKeyAndVisible];
// here, we could do some nice animation instead of just adding the subview...
}