2012-03-16 6 views
1

UIViewcontroller에 5 개의 이미지가 Storyboard에 있습니다. 5 개의 이미지 중 4 개를 손가락으로 화면 (그래프 1,2,3,4)으로 움직일 수 있습니다. 그런 다음 if 문을 만들었습니다. 고정 된 이미지 (최종 그래프) 중 하나를 드래그하면 레이블에 "텍스트"가 나타납니다. 내 진술에서 나는 절편을 썼다. 유일한 문제는 두 이미지가 가로 채 자마자 라벨의 텍스트가 나타나는 것입니다. 그러나 사용자가 손가락을 떼어내어 이미지를 "떨어 뜨리면"나타나게하고 싶습니다. 내가 어떻게 할 수 있니? 답변 주신 여러분 모두에게이벤트

//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