高级问题:如何在 UIView 中以编程方式创建的一系列 UILabel 对象的所有迭代中获得等效的功能(以跨 CGIntersect 和第二个系列的 UILabel 运行)?
以编程方式创建的 UILabel 系列的所有实例都可以拥有等效的标题(self.MYUILABEL)并保留等效的功能吗?
我使用 for 循环以编程方式制作了一系列等效分类的 UILabel,但只有最后一个 UILabel 实例被分配了 UILabel 标题。
如何在 UIView 中以编程方式创建的一系列 UILabel 对象的所有迭代中获得等效的功能?
目标是通过触摸将一系列 UILabel 移动到第二系列 UILabel。
在我看来,这是我制作 UILabel(充当目标区域)的方式。
for (int i=0;i<characterCount ;i++){
self.myBottomLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 200.0, 50.0, 50.0)];
self.myBottomLabel.backgroundColor = [UIColor whiteColor];
self.myBottomLabel.text= [myword objectAtIndex:i];
self.myBottomLabel.userInteractionEnabled = NO;
self.myBottomLabel.tag=300+[[letterToNumber objectForKey:[myword objectAtIndex:i]] integerValue];
[self.view insertSubview: self.myBottomLabel atIndex:(500)];
}
当我尝试使用 self.myBottomLabel 使用 CGRectIntersectsRect 时,只有最后一个 UILabel 才能与 thisCGRect 使用的 self.myBottomLabel 标题一起正常工作:
theReceivingCard = [self.myBottomLabel convertRect:[self.myBottomLabel frame] toView:self.view];
这里几乎是所有的实现。我是否需要创建多个 CGRect(我不知道该怎么做)?或者有什么方法可以准确地找到这些 UILabel 的标签(当您将相应的 UILabel 移动到它们的顶部时,它们会变成交互式的?)
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray* myword=[NSArray arrayWithObjects:@"h",@"e",@"l",@"l",@"o",nil];
NSDictionary *letterToNumber;
letterToNumber = [NSDictionary dictionaryWithObjectsAndKeys:
@"0", @"a",
@"1", @"b",
@"2", @"c",
@"3", @"d",
@"4", @"e",
@"5", @"f",
@"6", @"g",
@"7", @"h",
@"8", @"i",
@"9", @"j",
@"10", @"k",
@"11", @"l",
@"12", @"m",
@"13", @"n",
@"14", @"o",
@"15", @"p",
@"16", @"q",
@"17", @"r",
@"18", @"s",
@"19", @"t",
@"20", @"u",
@"21", @"v",
@"22", @"w",
@"23", @"x",
@"24", @"y",
@"25", @"z",
nil];
NSUInteger characterCount = [myword count];
//moveable
for (int i=0;i<characterCount ;i++){
self.myTopLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 100.0, 50.0, 50.0)];
self.myTopLabel.backgroundColor = [UIColor whiteColor];
self.myTopLabel.text= [myword objectAtIndex:i];
self.myTopLabel.userInteractionEnabled = YES;
self.myTopLabel.tag=100+[[letterToNumber objectForKey:[myword objectAtIndex:i]] integerValue];
[self.view insertSubview: self.myTopLabel atIndex:(1)];
}
//receiver
for (int i=0;i<characterCount ;i++){
self.myBottomLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 200.0, 50.0, 50.0)];
self.myBottomLabel.backgroundColor = [UIColor whiteColor];
self.myBottomLabel.text= [myword objectAtIndex:i];
self.myBottomLabel.userInteractionEnabled = NO;
self.myBottomLabel.tag=300+[[letterToNumber objectForKey:[myword objectAtIndex:i]] integerValue];
[self.view insertSubview: self.myBottomLabel atIndex:(500)];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];
[[viewYouWishToObtain superview] bringSubviewToFront:viewYouWishToObtain];
if ([touch view] != viewYouWishToObtain && viewYouWishToObtain.tag >= 100 && viewYouWishToObtain.tag <= 200) {
if ([touch tapCount] == 2) {
}
return;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];
if ([touch view] == viewYouWishToObtain && viewYouWishToObtain.tag >= 100 && viewYouWishToObtain.tag <= 200) {
CGPoint location = [touch locationInView:self.view];
viewYouWishToObtain.center = location;
theMovingCard = [viewYouWishToObtain convertRect:[viewYouWishToObtain frame] toView:self.view];
theReceivingCard = [self.myBottomLabel convertRect:[self.myBottomLabel frame] toView:self.view];
CGRect theZone = CGRectMake(theReceivingCard.origin.x, theReceivingCard.origin.y,theReceivingCard.size.width*2 , theReceivingCard.size.height*2);
CGRect theCard = CGRectMake(theMovingCard.origin.x, theMovingCard.origin.y,theMovingCard.size.width*2 , theMovingCard.size.height*2);
if(CGRectIntersectsRect(theCard, theZone))
{
NSLog(@"intersect");
}
return;
}
}
问题是在所有代码的末尾,intersect 仅在最后一个 UILabel 上起作用。我需要相交来处理所有充当接收器的 UILabel 系列。超级超级超级超级提前感谢。