Touch hotspot UIView like UIButton's with “Shows touch on highlight”

What is the best way to create programatically a semi-transparent UIView, looking like this:


http://i.stack.imgur.com/rJP1i.png

Basically the image shows results of pressing an UIButton with "Shows touch on highlight" option turned on. But I need to create a touch hotspot feature, which I will move over the screen while follow a translate gesture.

If I understood you want to move the highlight over the screen/view? Use UIPanGestureRecognizer and when pan starts add UIImageView with your highlight image in the main view. Then move the view when panning changes. If you want to mask the view, use UIView's maskView property (iOS 8 <) or UIView's layer's mask property (< iOS 8).

The part about UIPanGestureRecognizer has been clear to me - but thank you anyway.


My questions concerns rather how to create programatically the UIImage that radially shades off of from opaque white in the center to transparent close to the edges.

Here is some Core Graphics code I use to do the opposite (draw a partially transparent black overlay over the rest of the screen). But you could easily modify it to draw partially transparent white over a smaller area.


- (void)drawRect:(CGRect)rect
{
    CGRect targetRect = [self convertRect:self.target.bounds fromView:self.target];
   
    size_t num_locations = 3;
    CGFloat locations[3] = { 0.0, 0.4, 1.0 };
    CGFloat components[12] = {  0.0, 0.0, 0.0, 0.3,
                                0.0, 0.0, 0.0, 0.5,
                                0.0, 0.0, 0.0, 0.6 };
   
    CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef myGradient = CGGradientCreateWithColorComponents(myColorspace, components, locations, num_locations);
   
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    CGPoint centerPoint = CGPointMake(CGRectGetMidX(targetRect), CGRectGetMidY(targetRect));
    CGFloat startRadius = 0;
    CGFloat endRadius = 300;
    CGContextDrawRadialGradient (context, myGradient, centerPoint,
                                 startRadius, centerPoint, endRadius,
                                 kCGGradientDrawsAfterEndLocation);
    CGColorSpaceRelease(myColorspace);
    CGGradientRelease(myGradient);
}
Touch hotspot UIView like UIButton's with “Shows touch on highlight”
 
 
Q