-1

高度な質問: 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 のすべてで機能するには、交差が必要です。超超超超よろしくお願いします。

4

2 に答える 2

2

ループ内に大量のラベルを作成していますが、それらすべてを同じプロパティに割り当てるため、最後に作成したラベルへのポインタしかありません。残りの質問については完全にはわかりませんが、これらのラベルを配列に追加し、ヒットテストなどでその配列を反復処理する必要があると思います。

于 2012-09-01T18:18:30.977 に答える
0

これは、 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
于 2012-09-06T11:31:56.057 に答える