`
jsntghf
  • 浏览: 2479079 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

放大镜效果

    博客分类:
  • iOS
阅读更多

MagnifierView.h文件:

 

#import <Foundation/Foundation.h>

@interface MagnifierView : UIView {
	UIView *viewref;
	CGPoint touchPoint;
	UIImage *cachedImage;
}

@property(nonatomic, retain) UIView *viewref;
@property(assign) CGPoint touchPoint;
@end

 

MagnifierView.m文件:

 

#import "MagnifierView.h"

@implementation MagnifierView

@synthesize viewref, touchPoint;

- (id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		self.backgroundColor = [UIColor clearColor];
	}
	return self;
}

- (void)drawRect:(CGRect)rect {
	if(cachedImage == nil){
		UIGraphicsBeginImageContext(self.bounds.size);
		[self.viewref.layer renderInContext:UIGraphicsGetCurrentContext()];
		cachedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
		UIGraphicsEndImageContext();
	}
	CGImageRef imageRef = [cachedImage CGImage];
	CGImageRef maskRef = [[UIImage imageNamed:@"loopmask.png"] CGImage];
	CGImageRef overlay = [[UIImage imageNamed:@"loop.png"] CGImage];
	CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), 
																			CGImageGetHeight(maskRef),
																			CGImageGetBitsPerComponent(maskRef), 
																			CGImageGetBitsPerPixel(maskRef),
																			CGImageGetBytesPerRow(maskRef), 
																			CGImageGetDataProvider(maskRef), 
																			NULL, true);
	CGImageRef subImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(touchPoint.x - 18, touchPoint.y - 18, 36, 36));
	CGImageRef xMaskedImage = CGImageCreateWithMask(subImage, mask);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0,  0.0);
	CGContextConcatCTM(context, xform);
	CGRect area = CGRectMake(touchPoint.x - 42, -touchPoint.y, 85, 85);
	CGRect area2 = CGRectMake(touchPoint.x - 40, -touchPoint.y + 2, 80, 80);	
	CGContextDrawImage(context, area2, xMaskedImage);
	CGContextDrawImage(context, area, overlay);
}

- (void)dealloc {
	[cachedImage release];
	[viewref release];
	[super dealloc];
}

@end

 

TouchReader.h文件:

 

#import <Foundation/Foundation.h>
#import "MagnifierView.h"

@interface TouchReader : UIView {
	NSTimer *touchTimer;
	MagnifierView *loop;
}

- (void) handleAction:(id)timerObj;
@end

 

TouchReader.m文件:

 

#import "TouchReader.h"
#import "MagnifierView.h"

@implementation TouchReader

- (id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		touchTimer = nil;
		loop = nil;
	}
	return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	touchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
																								target:self
																							selector:@selector(handleAction:)
																							userInfo:touches
																							 repeats:NO];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
	if(loop != nil){
		[self handleAction:touches];
		return;
	}
	[touchTimer invalidate];
	touchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
																								target:self
																							selector:@selector(handleAction:)
																							userInfo:touches
																							 repeats:NO];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
	[touchTimer invalidate];
	touchTimer = nil;
	[loop removeFromSuperview];
	[loop release];
	loop = nil;
}

- (void) handleAction:(id)timerObj {
	NSSet *touches;
	if([timerObj isKindOfClass:[NSSet class]]){
		touches = [timerObj retain];
	}else{
		touches = [[timerObj userInfo] retain];
	}
	if(touchTimer != nil){
		[touchTimer invalidate];
		touchTimer = nil;
	}
	if(loop == nil){
		loop = [[MagnifierView alloc] initWithFrame:self.bounds];
		loop.viewref = self;
		[self addSubview:loop];
	}
	
	UITouch *touch = [touches anyObject];
	loop.touchPoint = [touch locationInView:self];
	[loop setNeedsDisplay];
	[touches release];
}

- (void)dealloc {
	[super dealloc];
}

@end

 

示例图:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics