ios 自制放大镜效果demo

MagnifierView.h
#import <UIKit/UIKit.h>

@interface MagnifierView : UIView {
//    CGPoint touchPoint;
}
@property (nonatomic, strong) UIView *viewToMagnify;
@property (nonatomic, assign) CGPoint touchPoint;
- (void)drawRect:(CGRect)rect;
@end

MagnifierView.m
#import "MagnifierView.h"

@implementation MagnifierView

- (void)setTouchPoint:(CGPoint)pt {
    _touchPoint = pt;

    self.center = CGPointMake(pt.x, pt.y-50);//跟随touchmove 不断得到中心点
}

- (void)drawRect:(CGRect)rect {

    //绘制放大镜效果部分

    CGContextRef context = UIGraphicsGetCurrentContext();//获取的是当前view的图形上下文
    CGContextTranslateCTM(context,1*(self.frame.size.width*0.5),1*(self.frame.size.height*0.5 + 50));//重新设置坐标系原点
    CGContextScaleCTM(context, 1.5, 1.5);//通过调用CGContextScaleCTM函数来指定x, y缩放因子 这里我们是扩大1.5倍
    CGContextTranslateCTM(context,-1*(_touchPoint.x),-1*(_touchPoint.y));
    [self.viewToMagnify.layer renderInContext:context];//直接在一个 Core Graphics 上下文中绘制放大后的图像,实现放大镜效果
}

@end


///
MagnifierView的使用
#import "ViewController.h"
#import "MagnifierView.h"

@interface ViewController () {
    MagnifierView *loop;
}
@property (nonatomic, strong) NSTimer *touchTimer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //计时器,手指点中0.5秒后启动放大镜效果
    self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                       target:self
                                                     selector:@selector(addLoop)
                                                     userInfo:nil
                                                      repeats:NO];

    if(loop == nil){
        loop = [[MagnifierView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        loop.viewToMagnify = self.view;
        loop.layer.borderColor = [UIColor grayColor].CGColor;
        loop.layer.borderWidth = 2;
        loop.layer.cornerRadius = 50;
        loop.layer.masksToBounds = YES;
    }

    UITouch *touch = [touches anyObject];
    loop.touchPoint = [touch locationInView:self.view];
    [loop setNeedsDisplay];
    [self.view addSubview:loop];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    //将手指移动信息传出给 handleAction
    [self handleAction:touches];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //手指抬起,将loop(放大镜)去掉
    [self.touchTimer invalidate];
    self.touchTimer = nil;

    [loop removeFromSuperview];
    loop = nil;
}

- (void)addLoop {
    [loop bringSubviewToFront:self.view];//让放大镜显示在最上层
}

- (void)handleAction:(id)timerObj {
    NSSet *touches = timerObj;
    UITouch *touch = [touches anyObject];
    loop.touchPoint = [touch locationInView:self.view];//将本身的touch信息传递给放大镜,设置放大镜的中心点
    [loop setNeedsDisplay];
//    loop drawRect:<#(CGRect)#>
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

 

转载于:https://my.oschina.net/zsyzone/blog/850186

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值