-1

高级问题:如何在 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 系列。超级超级超级超级提前感谢。

4

2 回答 2

2

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

于 2012-09-01T18:18:30.977 回答
0

这是通过 for 循环迭代“如何以编程方式为多个 CGIntersect 为 UIView 子集匹配创建多个 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
于 2012-09-06T11:31:56.057 回答