如何在程序创建的一系列UILabel对象的所有迭代中获得等效的功能



问题:如何在UIView中以编程方式创建的一系列UILabel对象的所有迭代中获得等效功能(使用第二系列UILabels在CGIntersect中运行)?

程序创建的UILabel系列的所有实例是否都具有相同的标题(self.MYUILABEL)并保留相同的功能?

我用for循环以编程方式制作了一系列等价分类的UILabel,但只有最后制作的UILabel实例被分配了UILabel标题。

如何在UIView中以编程方式创建的一系列UILabel对象的所有迭代中获得等效功能?

目标是通过触摸将一系列UILabels移动到第二系列UILabel。

以下是我如何在我的视图中创建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才能与此CGRect:使用的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系列。超级超级超级提前感谢。

您在循环中创建了大量标签,但您只有一个指向最后创建的标签的指针,因为您将它们都分配给了同一个属性。我还不完全清楚你的其他问题,但我认为你应该将这些标签添加到一个数组中,并在进行测试时迭代该数组。

以下是"如何通过for循环迭代,以编程方式为UIView子集匹配的多个CGIntersect创建多个CGRect值"的答案。

这是完整的文件。它是一个iOS触摸功能的基础,用于纸牌游戏(如纸牌游戏)或比赛游戏(儿童教育)。

该程序制作10张卡片,上面一行必须与下面一行相匹配。如果上排卡片的文本与下排卡片的相同,则CGIntersect将"命中",您可以执行任何其他方法。

应该有一种更简单的方法来制作变量CGRect,但我找不到答案。如果有人能想出如何不使用NSMutableArray和for循环来生成CGrect代码,那么如果你愿意分享,我将不胜感激。

.h

    #import <UIKit/UIKit.h>
    @interface ViewController : UIViewController
        @property NSString* currentCard;
        @property NSArray* myWord;
        @property NSString* myCurrentLetter;
        @property NSMutableArray* myMoverMutableArray;
        @property NSMutableArray* myReceiverMutableArray;
    @end

.m

    #import "ViewController.h"
    @interface ViewController ()
    {
        int myLetterTagNumber;
    }
    @end
    @implementation ViewController
        @synthesize currentCard = _currentCard;
        @synthesize myWord = _myWord;
        @synthesize myCurrentLetter = _myCurrentLetter;
        @synthesize myMoverMutableArray = _myMoverMutableArray;
        @synthesize myReceiverMutableArray = _myReceiverMutableArray;
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self writer];
    }
    -(void)writer{
        self.myWord=[NSArray arrayWithObjects:@"l",@"i",@"l",@"l",@"y",nil];
        NSUInteger characterCount = [self.myWord count];
        self.myMoverMutableArray = [NSMutableArray arrayWithCapacity:characterCount];
        self.myReceiverMutableArray = [NSMutableArray arrayWithCapacity:characterCount];
        //moveable
        for (int i=0;i<characterCount ;i++){
            UILabel*myTopLabel;
            myTopLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 100.0, 50.0, 50.0)];
            myTopLabel.backgroundColor = [UIColor whiteColor];
            myTopLabel.text= [self.myWord objectAtIndex:i];
            myTopLabel.userInteractionEnabled = YES;
            myTopLabel.textAlignment = UITextAlignmentCenter;
            myTopLabel.tag=100+i;
            [self.myMoverMutableArray addObject:[self.myWord objectAtIndex:i]];
            [self.view insertSubview: myTopLabel atIndex:(100)];
        }
        //receiver
        for (int i=0;i<characterCount ;i++){
            UILabel*myBottomLabel;
            myBottomLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 200.0, 50.0, 50.0)];
            myBottomLabel.backgroundColor = [UIColor redColor];
            myBottomLabel.text= [self.myWord objectAtIndex:i];
            myBottomLabel.userInteractionEnabled = YES;
            myBottomLabel.textAlignment = UITextAlignmentCenter;
            myBottomLabel.tag=300+i;
            [self.myReceiverMutableArray addObject:[self.myWord objectAtIndex:i]];        
            [self.view insertSubview: myBottomLabel atIndex:(300)];
        }
    }
    - (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];
        //clipping
        [[viewYouWishToObtain superview] bringSubviewToFront:viewYouWishToObtain];
        myLetterTagNumber = viewYouWishToObtain.tag;
        UILabel * myTempUILabel = (UILabel*)[self.view viewWithTag:(myLetterTagNumber)];
        self.myCurrentLetter = myTempUILabel.text;
        NSLog (@"text: %@",myTempUILabel.text);
        NSLog (@"Tag: %d",myLetterTagNumber);
        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;
            //moving card rec
            CGRect theMovingCard;
            theMovingCard = [viewYouWishToObtain convertRect:[viewYouWishToObtain frame] toView:self.view];
            CGRect themovingRect = CGRectMake(theMovingCard.origin.x, theMovingCard.origin.y,theMovingCard.size.width*2 , theMovingCard.size.height*2);
            NSString *myTempString;
            NSUInteger characterCount = [self.myWord count];
            for (int i=0;i<characterCount ;i++){
                myTempString= [self.myReceiverMutableArray objectAtIndex:i];
                if (myTempString == self.myCurrentLetter){
                    UILabel * mytouchMoveUILabel = (UILabel*)[self.view viewWithTag:(i+300)];
                    CGRect theReceivingCard;
                    theReceivingCard =  [mytouchMoveUILabel convertRect:[mytouchMoveUILabel frame] toView:self.view];
                    CGRect spaceToHitRect = CGRectMake(theReceivingCard.origin.x, theReceivingCard.origin.y,theReceivingCard.size.width*2 , theReceivingCard.size.height*2);
                    if(CGRectIntersectsRect(themovingRect, spaceToHitRect))
                    {
                        NSLog (@"Intersect: %d",myLetterTagNumber);
                    }
                }//if
            }//for
            return;
        }
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        // NSLog(@"3");
        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) {
            return;
        }
    }
                - (void)viewDidUnload
                {
                    [super viewDidUnload];
                    // Release any retained subviews of the main view.
                }
                - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
                {
                    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
                        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
                    } else {
                        return YES;
                    }
                }
    @end

最新更新