ページ 11

メソッド解放について xcode

Posted: 2013年9月14日(土) 17:26
by uu
xcodeにて画面遷移(画面1、画面2(画面2には画面1へ戻るボタンを作る))があるとして、画面2においてタッチメソッド(touchesBegan,touchesEnded)を使用すると、画面1に戻った際にもタッチメソッドが有効になってしまうんですが、どのようにすれば画面1では使えないようにできますでしょうか? 解放?みたいのを使うのでしょうか?

Re: メソッド解放について xcode

Posted: 2013年9月16日(月) 18:26
by しひ
状況がよくわからないので、画面遷移の方法とタッチメソッドの実装などを教えて頂けますか?

Re: メソッド解放について xcode

Posted: 2013年10月13日(日) 23:25
by iphonekunn
返信が送れてしまい申し訳ありません。

画面遷移の方法としては、画面1から画面2へは進むボタンを押すと遷移し画面2から画面1へは戻るボタンをおすと遷移という方法なのですが、画面2において以下のタッチメソッドをしようして、戻るボタンをおし画面1へ戻ってもタッチメソッドが使用できてしまうという現象が起きてしまいます。。

コード:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // タッチした座標を取得。
    CGPoint currentPoint = [[touches anyObject] locationInView:self.cavas];
    
    date = [NSDate date];
   
    // ボタン上の場合は処理を終了
    if (CGRectContainsPoint(self.clear.frame, currentPoint)
        || CGRectContainsPoint(self.regi.frame,currentPoint)){
        return;
    }
    
    if(currentPoint.y < 0 || currentPoint.y > 140){
        return;
    }
    
    // パスを初期化。
    bezierPath = [UIBezierPath bezierPath];
    bezierPath.lineCapStyle = kCGLineCapRound;
    bezierPath.lineWidth = 1.0;
   [bezierPath moveToPoint:currentPoint];
    
    currentPoint = [[touches anyObject]locationInView:self.cavas];
    NSLog(@"touchesBegan:(%.0f, %.0f)", currentPoint.x, currentPoint.y);
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // タッチ開始時にパスを初期化していない場合は処理を終了。
    if (bezierPath == nil){
        return;
    }
    
    // タッチした座標を取得。
    CGPoint currentPoint = [[touches anyObject] locationInView:self.cavas];
    
    if(currentPoint.y < 0 || currentPoint.y >140){
        return;
    }
    
    // パスにポイントを追加。
    [bezierPath addLineToPoint:currentPoint];
    
    // 線を描画。
    [self drawLine:bezierPath];
    
    currentPoint = [[touches anyObject]locationInView:self.cavas];
    NSLog(@"touchesMoved:(%.0f,%.0f)", currentPoint.x,currentPoint.y);
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // タッチ開始時にパスを初期化していない場合は処理を終了。
    if (bezierPath == nil){
        return;
    }
    
    // タッチした座標を取得。
    CGPoint currentPoint = [[touches anyObject] locationInView:self.cavas];
    
    if(currentPoint.y < 0 || currentPoint.y > 140){
        return;
    }
    
    // パスにポイントを追加。
    [bezierPath addLineToPoint:currentPoint];
    
    // 線を描画。
    [self drawLine:bezierPath];
    
    // 今回描画した画像を保持。
    lastDrawImage = self.cavas.image;
    
    // パスを保持。
    [Stack addObject:bezierPath];
    bezierPath = nil;
    
    self.regi.enabled = YES;
    
    currentPoint = [[touches anyObject]locationInView:self.cavas];
    NSLog(@"touchesEnded:(%.0f,%.0f)", currentPoint.x,currentPoint.y);
    
    NSInteger size = [Stack count];
    
    [kakusuu1 addObject:[NSNumber numberWithInt:size]];
    NSLog(@"%@",kakusuu1);
    
    
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
    
    [ontabtime addObject:[NSNumber numberWithFloat:interval]];
    NSLog(@"%@",ontabtime);
    
}