以前也写过一篇:放大镜效果,现在,提供另一种解决方案。
MagnifierView.h
@interface MagnifierView : UIWindow { UIView *viewToMagnify; CGPoint touchPoint; } @property (nonatomic, retain) UIView *viewToMagnify; @property (assign) CGPoint touchPoint; @end
MagnifierView.m
#import "MagnifierView.h" #import <QuartzCore/QuartzCore.h> @implementation MagnifierView @synthesize viewToMagnify, touchPoint; - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:CGRectMake(0, 0, 80, 80)]) { self.layer.borderColor = [[UIColor lightGrayColor] CGColor]; self.layer.borderWidth = 3; self.layer.cornerRadius = 40; self.layer.masksToBounds = YES; self.windowLevel = UIWindowLevelAlert; } return self; } - (void)setTouchPoint:(CGPoint)pt { touchPoint = pt; self.center = CGPointMake(pt.x, pt.y-10); } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, self.frame.size.width * 0.5, self.frame.size.height * 0.5); CGContextScaleCTM(context, 1.5, 1.5); CGContextTranslateCTM(context, -1 * (touchPoint.x), -1 * (touchPoint.y)); [self.viewToMagnify.layer renderInContext:context]; } - (void)dealloc { [viewToMagnify release]; [super dealloc]; } @end
TouchReader.h
#import <UIKit/UIKit.h> #import "MagnifierView.h" @interface TouchReader : UIView { NSTimer *touchTimer; MagnifierView *loop; } @property (nonatomic, retain) NSTimer *touchTimer; - (void)addLoop; - (void)handleAction:(id)timerObj; @end
TouchReader.m
#import "TouchReader.h" #import "MagnifierView.h" @implementation TouchReader @synthesize touchTimer; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { self.clipsToBounds = NO; self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addLoop) userInfo:nil repeats:NO]; 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 { [self handleAction:touches]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self.touchTimer invalidate]; self.touchTimer = nil; [loop release]; loop = nil; } - (void)addLoop { [loop makeKeyAndVisible]; } - (void)handleAction:(id)timerObj { NSSet *touches = timerObj; UITouch *touch = [touches anyObject]; loop.touchPoint = [touch locationInView:self]; [loop setNeedsDisplay]; } - (void)dealloc { [loop release]; loop = nil; [super dealloc]; } @end