高度な質問: UIView でプログラムによって作成された一連の UILabel オブジェクトのすべての反復で同等の機能を取得するにはどうすればよいですか (2 番目の一連の UILabels と CGIntersect で機能するため)?
プログラムで作成された UILabel シリーズのすべてのインスタンスは、同等のタイトル (self.MYUILABEL) を持ち、同等の機能を保持できますか?
forループを使用して一連の同等にクラス化されたUILabelsをプログラムで作成しましたが、UILabelの最後に作成されたインスタンスのみにUILabelタイトルが割り当てられます。
UIView でプログラムによって作成された一連の UILabel オブジェクトのすべての反復で同等の機能を得るにはどうすればよいですか?
目標は、一連の UILabels を 2 番目の一連の UILabels にタッチで移動させることです。
私のビューでUILabels(ゴールゾーンとして機能する)を作成した方法は次のとおりです。
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;
}
}
問題は、すべてのコードの最後で、交差が最後に作成された UILabel でのみ機能することです。レシーバーとして機能する一連の UILabels のすべてで機能するには、交差が必要です。超超超超よろしくお願いします。