iPhone, reproduce the magnifier effect

前端 未结 3 1585
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 11:45

I would like be able to create a movable magnifier (like the one you have when you copy and paste) in a custom view, for zooming a part of my view.

I have no idea on

3条回答
  •  感情败类
    2020-12-04 12:20

    We do this in Crosswords. In your drawRect method, mask off a circle (using a monochrome bitmap containing the 'mask' of your magnifying glass) and draw your subject view in there with a 2x scale transform. Then draw a magnifying glass image over that and you're done.

    - (void) drawRect: (CGRect) rect {
        CGContextRef    context = UIGraphicsGetCurrentContext();
        CGRect          bounds = self.bounds;
        CGImageRef      mask = [UIImage imageNamed: @"loupeMask"].CGImage;
        UIImage         *glass = [UIImage imageNamed: @"loupeImage"];
    
        CGContextSaveGState(context);
        CGContextClipToMask(context, bounds, mask);
        CGContextFillRect(context, bounds);
        CGContextScaleCTM(context, 2.0, 2.0);
    
        //draw your subject view here
    
        CGContextRestoreGState(context);
    
        [glass drawInRect: bounds];
    }
    

提交回复
热议问题