我创建了两个类1UIViewControllerClass
及其1UIViewClass
(即 View of the ViewController
)。在我的UIViewClass
我有两种方法,其中之一是touchesBegan
,它是获取接触点的位置。仅在视图上运行的第二个 Methode 获取位置信息并获取视图中该位置的颜色。第二个 Methode 返回一个UIColor
.
在这些步骤之后,UIColor
应该将其发送到UIViewController
. 我试图将UIColor
变量传递给ViewController
(委托和实例化),但没有任何效果。
代码如下。
更新:尝试了一个答案,但没有奏效。更新了此代码。
这里是FarbView.h
:
#import <UIKit/UIKit.h>
@protocol FarbDelegate <NSObject>
@required
- (void)receiveNewColor:(UIColor*)color;
@end
@interface FarbView :UIView {
__weak id <FarbDelegate> delegate;
}
@property (nonatomic, weak) id <FarbDelegate> delegate;
@property (strong,nonatomic) UIColor *pickedColor;
- (UIColor *) colorOfPoint:(CGPoint)point;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end
这里是FarbView.m
:
#import "FarbView.h"
#import <QuartzCore/QuartzCore.h>
@implementation FarbView
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
//Get Location of touched Point
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.pickedColor = [[UIColor alloc]init];
UITouch *touch = [[event allTouches] anyObject];
CGPoint loc = [touch locationInView:self];
NSLog(@"%@", NSStringFromCGPoint(loc));
self.pickedColor = [self colorOfPoint:loc];
//if you will describe receiveNewColor method on your view controller class we send new color message.
if([delegate respondsToSelector:@selector(receiveNewColor:)]){
[delegate receiveNewColor:self.pickedColor];
}
}
//Getting Color at Location
- (UIColor *) colorOfPoint:(CGPoint)point
{
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);
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;
}
接下来是FarbViewController.h
#import <UIKit/UIKit.h>
#import "FarbView.h"
@interface FarbViewController:UIViewController <FarbDelegate>
@property (strong, nonatomic) IBOutlet UILabel *currentColor;
@property (strong, nonatomic) FarbView *farbview;
-(void)receiveNewColor:(UIColor *)color;
@end
和FarbViewController.m
#import "FarbViewController.h"
@interface FarbViewController ()
@end
@implementation FarbViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Richtige Page 1");
self.farbview =[[FarbView alloc]init];
self.farbview.delegate = self;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)receiveNewColor:(UIColor *)color{
NSLog(@"New color selected %@", color);
//your code here
}
@end