Noh tein erään oppaan mukaan tämmöisen piirto-ohjelman ja nyt tähän pitäisi saada sellainen toiminto, että se quartzin avulla kirjottas isolla tuohon päälle. Eli tämmöstä oon saanu aikaan:
MainView.h
Koodi: Valitse kaikki
#import <UIKit/UIKit.h>
@interface MainView : UIView {
CGContextRef _myContext;
}
@end
MainView.m
Koodi: Valitse kaikki
#import "MainView.h"
@implementation MainView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
self.backgroundColor = [UIColor yellowColor];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
_myContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8, 4 *self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
CGContextSetLineWidth(_myContext, 10.0);
CGContextSetLineCap(_myContext, kCGLineCapRound);
CGContextSetRGBStrokeColor(_myContext, 1.0, 1.0, 1.0, 1.0);
}
return self;
}
void MyDrawText (CGContextRef myContext, CGRect contextRect) // 1
{
float w, h;
w = contextRect.size.width;
h = contextRect.size.height;
// CGAffineTransform myTextTransform; // 2
CGContextSelectFont (myContext, // 3
"Times-Bold",
h/10,
kCGEncodingMacRoman);
CGContextSetCharacterSpacing (myContext, 10); // 4
CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); // 5
CGContextSetRGBFillColor (myContext, 0, 1, 0, .5); // 6
CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1); // 7
// myTextTransform = CGAffineTransformMakeRotation (MyRadians (45)); // 8
// CGContextSetTextMatrix (myContext, myTextTransform); // 9
CGContextShowTextAtPoint (myContext, 40, 0, "Quartz 2D", 9); // 10
}
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGImageRef image = CGBitmapContextCreateImage(_myContext);
CGContextDrawImage(currentContext, [self bounds], image);
CGImageRelease(image);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint FirstLocation = [touch previousLocationInView:self];
CGPoint SecondLocation = [touch locationInView:self];
CGContextBeginPath(_myContext);
CGContextMoveToPoint(_myContext, FirstLocation.x, FirstLocation.y);
CGContextAddLineToPoint(_myContext, SecondLocation.x, SecondLocation.y);
CGContextStrokePath(_myContext);
MyDrawText(_myContext, [self bounds]);
[self setNeedsDisplay];
}
- (void)dealloc {
[super dealloc];
}
@end
Koodi: Valitse kaikki
#import <UIKit/UIKit.h>
@class MainView;
@interface QuartztestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MainView * mainView;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) MainView *mainView;
@end
Koodi: Valitse kaikki
#import "QuartztestAppDelegate.h"
#import "MainView.h"
@implementation QuartztestAppDelegate
@synthesize window;
@synthesize mainView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mainView = [[MainView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[window addSubview:mainView];
[window makeKeyAndVisible];
}
- (void)dealloc {
[mainView release];
[window release];
[super dealloc];
}
@end
Eli tuommosta. Tuolla on varmaa jotai ylimäärästäki ku on tullu kokeiltua monenlaista. Mutta mikäs mättää kun tuo "Quartz 2D" teksti ei ilmesty? Piirto siis toimii moitteetta.
Olisi mahtavaa jos joku osaisi/viitsisi jelppiä alottelijaa
-Mikko