本文實現了放大鏡功能,具體用法如下
1、將MagnifierView類導入到工程中,在例子中就有的。下載地址:http://pan.baidu.com/s/1eUz2Q
ViewController.h
#import#import "MagnifierView.h" @interface ViewController : UIViewController @property (strong , nonatomic) MagnifierView* loop; @property (strong , nonatomic) NSTimer* touchTimer; @end
- (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... }