Hallo,
ich habe versucht den Color Picker von v-vent.com/blog/?p=27 in meine App einzubinden.
Ich möchte mit meiner bestehenden Klasse und *.xib arbeiten.
In die *.xib habe ich eine neue View (colorPickerView) und ein ImageView (crossHairs) hinzugefügt.
Die View ist hidden und soll sich erst sichtbar schalten, wenn ein Button gedrückt wird.
Soweit, sogut. In meiner *.h habe ich:
Alles anzeigen
Außerdem habe ich die Constants.h aus dem Sample Code eingefügt und die Werte an meine Bedürfnisse angepasst:
Alles anzeigen
Den *.m Sample Code habe ich ebenfalls an meine Bedürfnisse angepasst, was dann so aussieht:
Alles anzeigen
Ich setze dann bei dem Button Event einfach :
Eigentlich sollte das dann ungefähr so aussehen:
osxentwicklerforum.de/index.php/Attachment/2399/
Bei mir wird eine schwarze View nur mit dem Fadenkreuz angezeigt (so wie in Interface Builder erstellt).
Sobald man das Fadenkreuz berührt stürzt die App ab:
Dabei habe ich ja nur den Sample Code (der einwandfrei funktioniert) kopiert und leicht angepasst.
Vielleicht mag sich das ja mal einer anschauen, wär nett.
Gruß
ich habe versucht den Color Picker von v-vent.com/blog/?p=27 in meine App einzubinden.
Ich möchte mit meiner bestehenden Klasse und *.xib arbeiten.
In die *.xib habe ich eine neue View (colorPickerView) und ein ImageView (crossHairs) hinzugefügt.
Die View ist hidden und soll sich erst sichtbar schalten, wenn ein Button gedrückt wird.
Soweit, sogut. In meiner *.h habe ich:
Quellcode
Außerdem habe ich die Constants.h aus dem Sample Code eingefügt und die Werte an meine Bedürfnisse angepasst:
Quellcode
Den *.m Sample Code habe ich ebenfalls an meine Bedürfnisse angepasst, was dann so aussieht:
Quellcode
- /* ColorPicker */
- #import "Constants.h"
- /* ColorPicker */
- @synthesize currentHue;
- @synthesize currentSaturation;
- - (id)initWithCoder:(NSCoder *)coder {
- if (self = [super initWithCoder:coder]) {
- [colorPickerView setMultipleTouchEnabled:YES];
- colorMatrixFrame = kHueSatFrame;
- UIImageView *hueSatImage = [[UIImageView alloc] initWithFrame:colorMatrixFrame];
- [hueSatImage setImage:[UIImage imageNamed:kHueSatImage]];
- [colorPickerView addSubview:hueSatImage];
- [colorPickerView sendSubviewToBack:hueSatImage];
- [hueSatImage release];
- currentColor = [[UIColor alloc] init];
- }
- return self;
- }
- - (NSString *)hexStringFromColor:(CGColorRef)theColor {
- const CGFloat *c = CGColorGetComponents(theColor);
- CGFloat r, g, b;
- r = c[0];
- g = c[1];
- b = c[2];
- // Fix range if needed
- if (r < 0.0f) r = 0.0f;
- if (g < 0.0f) g = 0.0f;
- if (b < 0.0f) b = 0.0f;
- if (r > 1.0f) r = 1.0f;
- if (g > 1.0f) g = 1.0f;
- if (b > 1.0f) b = 1.0f;
- // Convert to hex string between 0x00 and 0xFF
- return [NSString stringWithFormat:@"#%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
- }
- - (void)getStringForRGB:(CGColorRef)theColor {
- const CGFloat *c = CGColorGetComponents(theColor);
- CGFloat r, g, b;
- r = c[0];
- g = c[1];
- b = c[2];
- [red setValue:[[NSString stringWithFormat:@"%d", (int)(r * 255)] floatValue]];
- [green setValue:[[NSString stringWithFormat:@"%d", (int)(g * 255)] floatValue]];
- [blue setValue:[[NSString stringWithFormat:@"%d", (int)(b * 255)] floatValue]];
- }
- - (void)updateHueSatWithMovement:(CGPoint)position {
- currentHue = (position.x - kXAxisOffset) / kMatrixWidth;
- currentSaturation = 1.0 - (position.y - kYAxisOffset) / kMatrixHeight;
- [self getStringForRGB:currentColor.CGColor];
- }
- - (void)animateView:(UIImageView *)theView toPosition:(CGPoint)thePosition {
- [UIView beginAnimations:nil context:NULL];
- [UIView setAnimationDuration:kAnimationDuration];
- // Set the center to the final postion
- theView.center = thePosition;
- // Set the transform back to the identity, thus undoing the previous scaling effect.
- theView.transform = CGAffineTransformIdentity;
- [UIView commitAnimations];
- }
- - (void)dispatchTouchEvent:(CGPoint)position {
- if (CGRectContainsPoint(colorMatrixFrame,position)) {
- [self animateView:crossHairs toPosition:position];
- [self updateHueSatWithMovement:position];
- }
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- for (UITouch *touch in touches) {
- [self dispatchTouchEvent:[touch locationInView:colorPickerView]];
- }
- }
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- for (UITouch *touch in touches){
- [self dispatchTouchEvent:[touch locationInView:colorPickerView]];
- }
- }
Ich setze dann bei dem Button Event einfach :
Eigentlich sollte das dann ungefähr so aussehen:
osxentwicklerforum.de/index.php/Attachment/2399/
Bei mir wird eine schwarze View nur mit dem Fadenkreuz angezeigt (so wie in Interface Builder erstellt).
Sobald man das Fadenkreuz berührt stürzt die App ab:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -CGColor not defined for the UIColor <UIPlaceholderColor: 0x4d280b0>; need to first convert colorspace.'
Dabei habe ich ja nur den Sample Code (der einwandfrei funktioniert) kopiert und leicht angepasst.
Vielleicht mag sich das ja mal einer anschauen, wär nett.
Gruß

