我试图实现一个简单的颜色选择器组成的图像在UIImageView
。当用户在手指上拖动手指时,我想让相邻视图以当前像素颜色持续更新。颜色选择器的窘境
我发现了什么看起来像this question on SO东西接近@ rdelmar的回答了理想的解决方案,所以我借的代码,并修改它的pickedColor
发回的AddCategoryViewController
的方法。但是,此方法(setColorview
)未更新colorView
。
我的断点和NSLog显示颜色信息是从触摸收集的,发送和接收的,但显然没有应用,如受影响的UIView colorView
不反映颜色的事实所示。最明显的问题是,物业colorView
读取null
,我不明白为什么。
作为一个奖励,我真的很喜欢挑剔响应滑动的手指,而不仅仅是一个水龙头。
我已经包括以下所有相关的代码,具有重要的线标有// *******************
为寻找谢谢!所有帮助赞赏!
代码从这里开始,文件被---------------------------分离:
//
// UIView+UIView_ColorOfPoint.m
// WMDGx
//
//
#import "UIView+UIView_ColorOfPoint.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (UIView_ColorOfPoint)
-(UIColor *) colorOfPoint:(CGPoint)point
{
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel,
1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIColor *color = [UIColor colorWithRed:pixel[0]/255.0
green:pixel[1]/255.0 blue:pixel[2]/255.0
alpha:pixel[3]/255.0];
return color;
}
@end
-----------------------------------------
//
// ColorPickerView.m
// WMDGx
//
//
#import "ColorPickerView.h"
AddCategoryViewController *addCatVC;
@implementation ColorPickerView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint loc = [touch locationInView:self];
self.pickedColor = [self colorOfPoint:loc];
NSLog(@"Touches began");
//******************** For sending color info to setColorView in AddCategoryViewController
addCatVC = [[AddCategoryViewController alloc]init];
addCatVC.colorForColorView = self.pickedColor;
NSLog(@"Picked color is %@",self.pickedColor);
//******************** Call the method in AddCategoryViewController, which should display the color of the current pixel
[addCatVC setColorview];
}
@end
--------------------------------
//
// AddCategoryViewController.m
// WMDGx
//
//
#import "AddCategoryViewController.h"
@interface AddCategoryViewController()
@end
@implementation AddCategoryViewController
NSMutableArray *array;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.catTextField.delegate = self;
[self.pickerImageView setUserInteractionEnabled:YES];
self.colorView.layer.cornerRadius = 6;
// ********************I put this NSLog here to check the status of the property self.colorView
NSLog(@"Color view is %@",self.colorView);//_colorView UIView * 0x8a63d30 0x08a63d30 (from Variables View)
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.catTextField resignFirstResponder];
return YES;
}
-(void)setColorview // ********************This is where the backgroundColor of self.colorView should be set
{
NSLog(@"colorForColorView is %@",self.colorForColorView);
NSLog(@"Color view is %@",self.colorView); //_colorView UIView * nil 0x00000000 (from Variables View)
[self.colorView setBackgroundColor:self.colorForColorView]; //colorView should display the color, but doesn't
NSLog(@"Color view is %@",self.colorView); //_colorView UIView * nil 0x00000000 (from Variables View)
NSLog(@"Color view color is %@",self.colorView.backgroundColor);
}
- (IBAction)saveButton:(UIBarButtonItem *)sender
{
if (self.catTextField.text.length < 1)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No category created"
message:@"Please create a new category"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else if (!self.colorView.backgroundColor)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No color selected"
message:@"Please select a color for your new category"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
self.thisCategory = [WMDGCategory MR_createInContext:localContext];
self.thisCategory.name = [self.catTextField.text uppercaseString];
self.thisCategory.color = self.colorView.backgroundColor;
[localContext MR_saveToPersistentStoreAndWait];
[self.thisCell setUserInteractionEnabled:NO];
[self.delegate addCatControllerDidSave];
}
}
- (IBAction)cancelButton:(UIBarButtonItem *)sender
{
[self.delegate addCatControllerDidCancel:self.thisCategory];
}
@end
酷!两个问题:1)你在哪里放两个代码块? 2)这是否消除了我所包含的UIView类别的需求? – rattletrap99
我有一个ColorPicker UIViewController,这个代码在ColorPicker类中。这确实消除了对类别的需求,因为我直接查询了我正在使用的图像,而不是渲染视图然后查询该像素。 (再次查看代码有点有趣,因为我没有从UIImageView中获取图像,我直接从文件中查询它。理论上,您可以直接在UIImageView('picker')中查询图像,但我没有没有经过测试。) –
谢谢!我会在早上把这个旋转一下,让你知道发生了什么。我仍然对这种空洞的看法感到沮丧。使用标签时结果相同。 – rattletrap99