1

我指的是这段代码 -在视图上创建遮罩效果 以产生刮刮卡效果。现在,我想在特定区域(100、100、70、50)被划伤时显示特定动画。

或者对于那种情况,我只是想知道用户是否已经刮掉了整个上层来显示动画。

我们能知道哪些区域被划伤,哪些没有被划伤吗?

虽然我已经提供了链接,但我还将包含希望以这种方式及时回复的代码。:)

#import "ScratchableView.h"


@implementation ScratchableView


- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {
    scratchable = [UIImage imageNamed:@"screen1.png"].CGImage;
    width = CGImageGetWidth(scratchable);
    height = CGImageGetHeight(scratchable);
    self.opaque = NO;
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();

    CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
    alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone );
    provider = CGDataProviderCreateWithCFData(pixels);


    CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
    CGContextFillRect(alphaPixels, frame);

    CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(alphaPixels, 50.0);
    CGContextSetLineCap(alphaPixels, kCGLineCapRound);

    CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
    scratched = CGImageCreateWithMask(scratchable, mask);

    CGImageRelease(mask);
    CGColorSpaceRelease(colorspace);
}
return self;
}

- (void)drawRect:(CGRect)rect {
CGContextDrawImage(UIGraphicsGetCurrentContext() , [self bounds] , scratched);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
UITouch *touch = [[event touchesForView:self] anyObject];
firstTouch = YES;
location = [touch locationInView:self];
}

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

if (firstTouch) {
    firstTouch = NO;
    previousLocation = [touch previousLocationInView:self];
} else {
    location = [touch locationInView:self];
    previousLocation = [touch previousLocationInView:self];
}

// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{  
UITouch *touch = [[event touchesForView:self] anyObject];
if (firstTouch) {
    firstTouch = NO;
    previousLocation = [touch previousLocationInView:self];


    [self renderLineFromPoint:previousLocation toPoint:location];
}


}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end {

CGContextMoveToPoint(alphaPixels, start.x, start.y);
CGContextAddLineToPoint(alphaPixels, end.x, end.y);
CGContextStrokePath(alphaPixels);
[self setNeedsDisplay];
}

- (void)dealloc {
CGContextRelease(alphaPixels);
CGImageRelease(scratchable);
CGDataProviderRelease(provider);
    [super dealloc];
}
4

0 回答 0