关闭

放大镜效果

标签: 放大镜界面uiview
139人阅读 评论(0) 收藏 举报
分类:

放大镜视图类
WXMagnifierView.h

@interface WXMagnifierView : UIWindow

//放大框
@property(nonatomic,strong)UIView * magnifyView;

//触摸点
@property(nonatomic)CGPoint pointTomagnify;

@end

WXMagnifierView.m

@interface WXMagnifierView ()

@property (nonatomic, strong) CALayer *contentLayer;

@end


@implementation WXMagnifierView

-(instancetype)init
{
    self = [super init];

    if (self) {

        self.frame = CGRectMake(0, 0, 100, 50);

        self.backgroundColor = [UIColor clearColor];

        self.layer.borderWidth = 1;

        self.layer.borderColor = [UIColor   lightGrayColor].CGColor;

        self.layer.cornerRadius = 5;

        self.layer.masksToBounds = YES;

        self.windowLevel = UIWindowLevelAlert;

        self.contentLayer = [CALayer layer];

        self.contentLayer.frame = self.bounds;

        self.contentLayer.delegate = self;

        self.contentLayer.contentsScale = [[UIScreen mainScreen] scale];

        [self.layer addSublayer:self.contentLayer];

    }


    return self;
}


#pragma mark set the point of magnifier
-(void)setPointTomagnify:(CGPoint)pointTomagnify
{
    _pointTomagnify = pointTomagnify;

    CGPoint center = CGPointMake(pointTomagnify.x, self.center.y);

    if (pointTomagnify.y > CGRectGetHeight(self.bounds) * 0.5) {

        center.y = pointTomagnify.y - CGRectGetHeight(self.bounds)/2;
    }

    self.center = center;

    [self.contentLayer setNeedsDisplay];
}

#pragma mark  invoke  by setNeedDisplay
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{

    float width = CGRectGetWidth(self.frame);

    float height = CGRectGetHeight(self.frame);

    //宽高
    CGContextTranslateCTM(ctx,width * 0.5, height * 0.5);

    //缩放比例
    CGContextScaleCTM(ctx, 1.5, 1.5);

    //x y 坐标转换
    CGContextTranslateCTM(ctx, -self.pointTomagnify.x , -self.pointTomagnify.y - 20);

    //截屏并显示
    [self.magnifyView.layer renderInContext:ctx];

}

在需要的viewControlller中


//定义一个放大镜
@property (strong, nonatomic)WXMagnifierView *magnifierView;
//长按手势,实现放大镜
    UILongPressGestureRecognizer * pressGesTure = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(magniFierAction:)];

    self.msgLabel.userInteractionEnabled = YES;

    [self.msgLabel addGestureRecognizer:pressGesTure];
    [self magniFierAction:pressGesTure];
#pragma 放大镜
- (void)magniFierAction:(UILongPressGestureRecognizer *)gesture
{

    NSLog(@"长按手势");

    //获取手势位置

    CGPoint point = [gesture locationInView:self];

    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
        {

            //设置放大镜位置

            [self magnifierPosition:point];

            //显示放大镜
            [self.magnifierView makeKeyAndVisible];


            break;
        }
        case UIGestureRecognizerStateChanged:
        {

            //设置放大镜位置

            [self magnifierPosition:point];

            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            //长按结束取消放大镜

            self.magnifierView.hidden  =  YES;


            break;
        }
        default:
            break;
    }



}



-(void)magnifierPosition:(CGPoint )position
{
    CGPoint  point = position;

    point.y -= 25;

    position  =  point;

    self.magnifierView.pointTomagnify = point;


}


#pragma mark lazy laod
-(WXMagnifierView *)magnifierView
{
    if (! _magnifierView) {

        _magnifierView = [[WXMagnifierView alloc]init];

        _magnifierView.magnifyView = self.window;
    }

    return _magnifierView;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20373次
    • 积分:1000
    • 等级:
    • 排名:千里之外
    • 原创:83篇
    • 转载:4篇
    • 译文:0篇
    • 评论:1条