了解CGContext是否包含特定区域-iPhone
我指的是本守则— 以产生刮卡效果。现在,我想显示一个特定的动画,当一个特定的区域(100,100,70,50)被划伤时 或者在这种情况下,我只想知道用户是否抓取了整个上层来显示动画 我们能知道哪些区域有划痕,哪些区域没有划痕吗 虽然我已经提供了链接,但我也将包含代码,希望通过这种方式得到快速回复。:)了解CGContext是否包含特定区域-iPhone,iphone,ios,ipad,uiimage,cgcontextref,Iphone,Ios,Ipad,Uiimage,Cgcontextref,我指的是本守则— 以产生刮卡效果。现在,我想显示一个特定的动画,当一个特定的区域(100,100,70,50)被划伤时 或者在这种情况下,我只想知道用户是否抓取了整个上层来显示动画 我们能知道哪些区域有划痕,哪些区域没有划痕吗 虽然我已经提供了链接,但我也将包含代码,希望通过这种方式得到快速回复。:) CGContext只是一个绘图区域——它没有边界,也不能与用户输入交互。一种方法是将UIView子类化并覆盖触摸处理方法。(在UIView的超类UIResponder中定义。的可能副本) #imp
CGContext只是一个绘图区域——它没有边界,也不能与用户输入交互。一种方法是将UIView子类化并覆盖触摸处理方法。(在UIView的超类UIResponder中定义。的可能副本)
#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];
}