<bdo id='4Ykvo'></bdo><ul id='4Ykvo'></ul>

      <legend id='4Ykvo'><style id='4Ykvo'><dir id='4Ykvo'><q id='4Ykvo'></q></dir></style></legend>

      <i id='4Ykvo'><tr id='4Ykvo'><dt id='4Ykvo'><q id='4Ykvo'><span id='4Ykvo'><b id='4Ykvo'><form id='4Ykvo'><ins id='4Ykvo'></ins><ul id='4Ykvo'></ul><sub id='4Ykvo'></sub></form><legend id='4Ykvo'></legend><bdo id='4Ykvo'><pre id='4Ykvo'><center id='4Ykvo'></center></pre></bdo></b><th id='4Ykvo'></th></span></q></dt></tr></i><div id='4Ykvo'><tfoot id='4Ykvo'></tfoot><dl id='4Ykvo'><fieldset id='4Ykvo'></fieldset></dl></div>

        <tfoot id='4Ykvo'></tfoot>
      1. <small id='4Ykvo'></small><noframes id='4Ykvo'>

        如何在自定义控件中添加放大镜?

        时间:2023-10-22

        <legend id='XPve6'><style id='XPve6'><dir id='XPve6'><q id='XPve6'></q></dir></style></legend>
      2. <i id='XPve6'><tr id='XPve6'><dt id='XPve6'><q id='XPve6'><span id='XPve6'><b id='XPve6'><form id='XPve6'><ins id='XPve6'></ins><ul id='XPve6'></ul><sub id='XPve6'></sub></form><legend id='XPve6'></legend><bdo id='XPve6'><pre id='XPve6'><center id='XPve6'></center></pre></bdo></b><th id='XPve6'></th></span></q></dt></tr></i><div id='XPve6'><tfoot id='XPve6'></tfoot><dl id='XPve6'><fieldset id='XPve6'></fieldset></dl></div>
      3. <tfoot id='XPve6'></tfoot>

                  <bdo id='XPve6'></bdo><ul id='XPve6'></ul>
                • <small id='XPve6'></small><noframes id='XPve6'>

                    <tbody id='XPve6'></tbody>

                • 本文介绍了如何在自定义控件中添加放大镜?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何为自定义控件添加放大镜?控件是 UIView 的子级.(这是一个 UIWebView - 但本机放大功能在某些页面上不起作用.)

                  How to add a magnifier to custom control? Control is a child of UIView. (It's a UIWebView - but native magnification functionality doesn't work at some pages.)

                  更新:

                  也许可以在 UIWebView 上强制绘制放大镜?

                  Maybe it's possible to force a draw of magnifier on UIWebView?

                  推荐答案

                  1.将以下文件添加到您的项目中:

                  放大镜视图.h:

                  1. Add the following files to your project:

                  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
                  

                  放大镜视图.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, 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:

                  //
                  //  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:

                  //
                  //  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

                  代码上使用的图片:

                  loupe-hi@2x.png:

                  loupe-hi@2x.png:

                  放大镜掩码@2x.png:

                  loupe-mask@2x.png:

                  带有阴影的原始但居中的图像(此时未使用):

                  Original but centered images with a shadow (not used at this moment):

                  loupe-shadow-hi@2x.png:

                  loupe-shadow-hi@2x.png:

                  loupe-shadow-mask@2x.png:

                  loupe-shadow-mask@2x.png:

                  放大镜将自动工作,但自身捕获触摸事件的控件除外(例如,UIWebView).并且上面的代码不支持带有阴影的图像.如果您成功解决了此问题,请为问题添加新答案.

                  The magnifier will work automaticaly except controls that captures touch events themselfs (for example, UIWebView). And the code above doesn't support the images with a shadow. Please add new answer to the qustion if you successfully fix this issue.

                  更新:

                  更改以下代码以添加 UIWebView 支持.UIView 应保留为 UIView.

                  Change the following code to add UIWebView support. UIView should remain 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];
                  

                  这篇关于如何在自定义控件中添加放大镜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有进度条的 UIWebView 下一篇:获取 UIWebView 内容的高度

                  相关文章

                    <tfoot id='XMuWq'></tfoot>
                  1. <i id='XMuWq'><tr id='XMuWq'><dt id='XMuWq'><q id='XMuWq'><span id='XMuWq'><b id='XMuWq'><form id='XMuWq'><ins id='XMuWq'></ins><ul id='XMuWq'></ul><sub id='XMuWq'></sub></form><legend id='XMuWq'></legend><bdo id='XMuWq'><pre id='XMuWq'><center id='XMuWq'></center></pre></bdo></b><th id='XMuWq'></th></span></q></dt></tr></i><div id='XMuWq'><tfoot id='XMuWq'></tfoot><dl id='XMuWq'><fieldset id='XMuWq'></fieldset></dl></div>

                      <legend id='XMuWq'><style id='XMuWq'><dir id='XMuWq'><q id='XMuWq'></q></dir></style></legend>

                        <bdo id='XMuWq'></bdo><ul id='XMuWq'></ul>

                      <small id='XMuWq'></small><noframes id='XMuWq'>