//
// ScratchableView.h
// CGScratch
//
// Created by Olivier Yiptong on 11-01-11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ScratchableView : UIView {
float width;
float height;
CGPoint location;
CGPoint previousLocation;
BOOL firstTouch;
CGImageRef scratchable;
CGImageRef scratched;
CGContextRef alphaPixels;
CGDataProviderRef provider;
}
- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end;
@end
//
// ScratchableView.m
// CGScratch
//
// Created by Olivier Yiptong on 11-01-11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ScratchableView.h"
@implementation ScratchableView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//self.bounds = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height);
//自行把用來刮的scratch.png放入專案
scratchable = [UIImage imageNamed:@"scratch.png"].CGImage;
//修改成隨bounds大小變化 by Harvey 20130903
// width = CGImageGetWidth(scratchable);
// height = CGImageGetHeight(scratchable);
width = self.bounds.size.width;
height = self.bounds.size.height;
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, 20.0);
CGContextSetLineCap(alphaPixels, kCGLineCapRound);
//CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
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];
}
@end
「中200!繼續買!」
回覆刪除「又中200!繼續繼續!」
「挖賽!怎麼一直中,我是不是要發了?」
「又中200?!這次來挑戰2000的!」
「……」
http://ck8.me/?tp=article&id=31886&1=mi800420&r=mi800420