大佬教程收集整理的这篇文章主要介绍了iphone – 如何将放大镜添加到自定义控件?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_265_5@magnifierView.h:
// // MagnifierView.h // SimplerMaskTest // #import <UIKit/UIKit.h> @interface MagnifierView : UIView { UIView *viewToMagnify; CGPoint touchPoint; } @property (nonatomic,retain) UIView *viewToMagnify; @property (assign) CGPoint touchPoint; @end@H_265_5@magnifierView.m:
// // MagnifierView.m // SimplerMaskTest // #import "MagnifierView.h" #import <QuartzCore/QuartzCore.h> @implementation MagnifierView @synthesize viewToMagnify; @dynamic touchPoint; - (id)initWithFrame:(CGRect)frame { return [self initWithFrame:frame radius:118]; } - (id)initWithFrame:(CGRect)frame radius:(int)r { int radius = r; if ((self = [super initWithFrame:CGRectMake(0,radius,radius)])) { //Make the layer circular. self.layer.cornerRadius = radius / 2; self.layer.masksToBounds = YES; } return self; } - (void)setTouchPoint:(CGPoint)pt { touchPoint = pt; // whenever touchPoint is set,update the position of the magnifier (to just above what's being magnified) self.center = CGPointMake(pt.x,pt.y-66); } - (CGPoint)getTouchPoint { return touchPoint; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGRect bounds = self.bounds; CGImageRef mask = [UIImage imagenamed: @"loupe-mask@2x.png"].CGImage; UIImage *glass = [UIImage imagenamed: @"loupe-hi@2x.png"]; CGContextSaveGState(context); CGContextClipToMask(context,bounds,mask); CGContextFillRect(context,bounds); CGContextScaleCTM(context,1.2,1.2); //draw your subject view here CGContextTranslateCTM(context,1*(self.frame.size.width*0.5),1*(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]; CGContextRestoreGState(context); [glass drawInRect: bounds]; } - (void)dealloc { [viewToMagnify release]; [super dealloc]; } @end
TouchReader.h:
// // TouchReader.h // SimplerMaskTest // #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:
// // TouchReader.m // SimplerMaskTest // #import "TouchReader.h" #import "MagnifierView.h" @implementation TouchReader @synthesize touchTimer; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 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 { [self handleAction:touches]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self.touchTimer invalidate]; self.touchTimer = nil; [loop removeFromSuperview]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [self.touchTimer invalidate]; self.touchTimer = nil; [loop removeFromSuperview]; } - (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]; // here,we Could do some nice animation instead of just adding the subview... } - (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
基于:http://coffeeshopped.com/2010/03/a-simpler-magnifying-glass-loupe-view-for-the-iphone
2.添加以下图像:
代码上使用的图像:
loupe-hi@2x.png:
loupe-mask@2x.png:
带阴影的原始但居中的图像(此时不使用):
loupe-shadow-hi@2x.png:
loupe-shadow-mask@2x.png:
3.用TouchReader替换xib文件中的主UIView
放大镜将自动工作,除了捕获自己的触摸事件的控件(例如,UIWebView).上面的代码不支持带阴影的图像.如果您成功解决了此问题,请为qustion添加新答案.
更新:
更改以下代码以添加UIWebView支持. UIView应该保持UIView.
@interface TouchReader : UILongPressGestureRecognizer
并向webView添加手势:
TouchReader* gestureMagnifier = [[[TouchReader alloc] initWithTarget:self action:@SELEctor(handleMagnifier:)] autorelease]; gestureMagnifier.webView = editsource; gestureMagnifier.delegate = self; gestureMagnifier.minimumPressDuration = 0.5; [webView addGestureRecognizer:gestureMagnifier];
以上是大佬教程为你收集整理的iphone – 如何将放大镜添加到自定义控件?全部内容,希望文章能够帮你解决iphone – 如何将放大镜添加到自定义控件?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。