我必须在下图中找到类似frame
的颜色UIView
:
在这张图片中,我想找到frame
三角形并想添加一个UIControl
。
我能做到吗?
创建具有两个属性的类:形状、颜色和图像以及UIImageView
具有新属性的子类:imageObject
. (我只是认为这YBImageObject
是一个可怕的类名,所以如果您打算使用此代码,请更改它)
在viewDidLoad
:
NSArray *images = @[@{@"name": @"darkgreen-circle1", @"shape" : @"Circle", @"color" : @"#8dc1b1"},
@{@"name": @"darkgreen-circle2", @"shape" : @"Circle", @"color" : @"#8dc1b1"},
@{@"name": @"darkgreen-circle3", @"shape" : @"Circle", @"color" : @"#8dc1b1"},
@{@"name": @"lightblue-square1", @"shape" : @"Square", @"color" : @"#00c1f5"},
@{@"name": @"lightblue-square2", @"shape" : @"Square", @"color" : @"#00c1f5"},
@{@"name": @"red-circle1", @"shape" : @"Circle", @"color" : @"#ff799a"},
@{@"name": @"yellow-triangle1", @"shape" : @"Triangle", @"color" : @"#e3ff86"},
@{@"name": @"yellow-triangle2", @"shape" : @"Triangle", @"color" : @"#e3ff86"}];
int i = 0;
for (NSDictionary *dictImage in images){
YBImageObject *imageObject = [[YBImageObject alloc] init];
[imageObject setImage:[UIImage imageNamed:[dictImage objectForKey:@"name"]]];
[imageObject setShape:[dictImage objectForKey:@"shape"]];
[imageObject setColor:[dictImage objectForKey:@"color"]];
// imageView.frame.origin.x = total width of previous images * a little margin
YBImageView *imageView = [[YBImageView alloc] initWithFrame:CGRectMake(i * 1.2, 100, imageObject.image.size.width, imageObject.image.size.height)];
[imageView setBackgroundColor:[UIColor blackColor]];
[imageView setImageObject:imageObject];
[self.view addSubview:imageView];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestures:)];
[imageView addGestureRecognizer:tapGestureRecognizer];
[imageView setUserInteractionEnabled:YES];
i += imageObject.image.size.width;
}
在handleTapGestures:
:
- (void) handleTapGestures:(UITapGestureRecognizer *)gestureRecognizer
{
YBImageView *imageView = (YBImageView *)gestureRecognizer.view;
NSLog(@"Shape: %@", imageView.imageObject.shape);
NSLog(@"Color: %@", imageView.imageObject.color);
}