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

时间:2012-09-01 17:45:46

标签: objective-c uilabel

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

以编程方式创建的UILabel系列的所有实例是否都具有等效标题(self.MYUILABEL)并保留等效功能?

我使用for循环以编程方式创建了一系列等效的UILabel,但只有UILabel的最后一个实例被分配了UILabel标题。

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

目标是通过触摸将一系列UILabel移动到第二系列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才能正确使用thisCGRect使用的self.myBottomLabel标题:

theReceivingCard = [self.myBottomLabel convertRect:[self.myBottomLabel frame] toView:self.view];

这几乎是所有的实现。我是否需要创建多个CGRects(我不知道该怎么做)?或者有没有找到这些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上起作用。我需要相交来处理作为接收器的所有UILabel系列。超级超级超级超级感谢提前。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

这是完整的文件。 它是一款支持iOS touch的纸牌游戏(如纸牌游戏)或比赛游戏(儿童教育)。

该程序制作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