2012-03-16 5 views
1

UIViewcontrollerに5画像あります。Storyboardです。 5つの画像のうちの4つを、画面上の指で動かすことができます(グラフ1,2,3,4)。次に、ifステートメントを作成しました。固定された画像(最終的なグラフ)の1つをドラッグすると、ラベルに「テキスト」が表示されます。私の声明では、私は傍受を書いた。唯一の問題は、2つの画像が傍受するとすぐにラベルのテキストが表示されることです。しかし、ユーザーが指を離して画像を「落とす」と表示されるようにしたい。どうやってやるの?ここで指リリースのイベント

は、コードは次のとおりです。答える人あなたのすべてに

//image movement 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 

    if ([touch view] == graph1) { 
     CGPoint location = [touch locationInView:self.view]; 
     graph1.center = location; 
     [self interceptofgraphs]; 
    } 

    if ([touch view] == graph2) { 
     CGPoint location = [touch locationInView:self.view]; 
     graph2.center = location; 
    [self interceptofgraphs]; 
    } 

    if ([touch view] == graph3) { 
     CGPoint location = [touch locationInView:self.view]; 
     graph3.center = location; 
     [self interceptofgraphs]; 
    } 

    if ([touch view] == graph4) { 
     CGPoint location = [touch locationInView:self.view]; 
     graph4.center = location; 
     [self interceptofgraphs]; 
    } 
} 

-(void)interceptofgraphs { 

    if (CGRectIntersectsRect(graph2.frame, finalgraph.frame)) 
     graphlabel.text = [NSString stringWithFormat: @"Right answer. Well done!!"]; 

    if (CGRectIntersectsRect(graph1.frame, finalgraph.frame)) 
     graphlabel.text = [NSString stringWithFormat: @"Wrong!! not at all."]; 

    if (CGRectIntersectsRect(graph3.frame, finalgraph.frame)) 
     graphlabel.text = [NSString stringWithFormat: @"Wrong!! Try again"]; 

    if (CGRectIntersectsRect(graph4.frame, finalgraph.frame)) 
     graphlabel.text = [NSString stringWithFormat: @"Wrong!! NO"]; 
} 

感謝!!

+0

。 XCodeの問題ではありません。 XCodeは単なるIDEであり、この質問はそれとは関係ありません。 – Almo

答えて

1

これは動作するはずです:

再びタグ付け
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 

    //we'll almost certainly need a location of touch 
    CGPoint location = [touch locationInView:self.view]; 

    //we store the reference of a view that was touched into touchedView 
    UIView *touchedView = [touch view]; 

    //if view that was touched is one of the views we're interested in 
    if ((touchedView == graph1)||(touchedView == graph2)|| 
     (touchedView == graph3)||(touchedView == graph4)) { 

    //we move it's center 
    //since the touchedView hold's a reference to the view that 
    //was touched - we're moving the right view for sure 
    touchedView.center = location;  
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    //we only want to check whitch view was moved at the end 
    //when user releases the touch 
    [self interceptofgraphs]; 
} 
+0

ありがとう!働いた! – Alessandro

+0

@Alessandro:ようこそ! –

+0

私は非常によくコードを理解していませんでした。 – Alessandro

関連する問題