2012-02-10 18 views
-1

我正在研究一個應用程序,該應用程序具有蒸起來的視圖,用戶可以用手指擦拭。我用我希望能夠工作的東西對它進行破解,但確實如此,但速度很慢。下面的代碼結合了繪圖代碼和屏蔽代碼。繪圖代碼繪製成黑白圖像,然後用作蒙版來掩蓋霧圖像。如何擦去蒸好的視圖

有沒有人知道任何示例代碼來實現這種效果,或有任何建議如何使其更快?

static CGPoint midPoint(CGPoint p1, CGPoint p2){ 
    return CGPointMake((p1.x+p2.x)*0.5f, (p1.y+p2.y)*0.5f); 
} 

- (UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)maskImage{ 
    CGImageRef maskRef = [maskImage CGImage]; 
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), 
             CGImageGetHeight(maskRef), 
             CGImageGetBitsPerComponent(maskRef), 
             CGImageGetBitsPerPixel(maskRef), 
             CGImageGetBytesPerRow(maskRef), 
             CGImageGetDataProvider(maskRef), NULL, false); 
    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask); 
    CGImageRelease(mask); 
    UIImage *maskedImage = [UIImage imageWithCGImage:masked]; 
    CGImageRelease(masked); 
    return maskedImage; 

} 

- (void)setMaskImage:(UIImage *)maskImage{ 
    [_maskImage release]; 
    _maskImage = [maskImage retain]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 

    _previousPoint1 = [touch previousLocationInView:self]; 
    _previousPoint2 = [touch previousLocationInView:self]; 
    _currentPoint = [touch locationInView:self]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 

    _previousPoint2 = _previousPoint1; 
    _previousPoint1 = [touch previousLocationInView:self]; 
    _currentPoint = [touch locationInView:self]; 

    CGPoint mid1 = midPoint(_previousPoint1, _previousPoint2); 
    CGPoint mid2 = midPoint(_currentPoint, _previousPoint1); 

    CGRect imageRect = CGRectZero; 
    imageRect.size = _fogView.frame.size; 
    UIGraphicsBeginImageContext(imageRect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [_maskImage drawInRect:imageRect]; 

    CGContextMoveToPoint(context, mid1.x, mid1.y); 
    CGContextAddQuadCurveToPoint(context, _previousPoint1.x, _previousPoint1.y, mid2.x, mid2.y); 

    CGContextSetLineCap(context, kCGLineCapRound); 
    CGContextSetLineWidth(context, 40.0f); 
    CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 1.0f); 
    CGContextStrokePath(context); 

    [self setMaskImage:UIGraphicsGetImageFromCurrentImageContext()]; 
    UIGraphicsEndImageContext(); 

    _fogView.image = [self maskImage:[UIImage imageNamed:@"Fog"] withMask:_maskImage]; 
} 

回答

0

@badeen幫助下,解決方案是使用兩個CALayers,一個是霧翳圖像,一個用於面膜,面膜繪製當用戶在屏幕上移動他們的手指插入。