2011-07-06 4 views
1

私はiOS開発にはかなり新しく、アプリケーションを開発するのに苦労しています。マルチビューアプリでスワイプジェスチャーを使用できません。私はコードを貼り付けています、皆さんはエラーを指摘してください。 ビューベースのアプリケーションです。ここでマルチビューAppのスワイプジェスチャー

がmainViewController.h

#import <UIKit/UIKit.h> 

@interface mainViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> 
{ 
     NSArray *listData; 
} 
@property (nonatomic, retain) NSArray *listData; 
@end 

今mainViewController.m

#import "mainViewController.h" 

@implementation mainViewController 
@synthesize listData; 


- (void)viewDidLoad { 
    NSArray *array =[[NSArray alloc] initWithObjects:@"Apple",@"Boy",@"Cat", nil]; 
    self.listData = array; 
    [array release]; 
    [super viewDidLoad]; 
} 


- (void)dealloc 
{ 
    [listData release]; 
    [super dealloc]; 
} 


#pragma mark - View lifecycle 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    return [self.listData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
          SimpleTableIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 
    //cell.textLabel.font = [UIFont boldSystemFontOfSize:50]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = [indexPath row]; 
    NSString *rowValue = [listData objectAtIndex:row]; 
    if([rowValue isEqualToString:@"Apple"]) 
    { 
    cell.backgroundColor = [UIColor redColor]; 
    } 
    else 
     if([rowValue isEqualToString:@"Boy"]) 
        cell.backgroundColor= [UIColor yellowColor]; 



} 

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = [indexPath row]; 
    NSString *rowValue = [listData objectAtIndex:row]; 
    if([rowValue isEqualToString:@"Apple"]) 
     { 
      mainViewController* flashView=[[mainViewController alloc] initWithNibName:@"fl" bundle:[NSBundle mainBundle]]; 
      [self.view addSubview:flashView.view]; 


     } 

    //[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    self.listData=nil; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 110; 
} 


@end 

である私は、 "FL" という名前のXIBファイルでUIViewSubControllerクラスを追加しました。 fl.hで

:fl.mで

#define kMinimumLength 5 
#define kMaxVariance 1 
#import <UIKit/UIKit.h> 
#import "mainViewController.h" 

@protocol flDelegate; 

@interface fl : UIViewController <UIGestureRecognizerDelegate> 
{ 

    CGPoint gestureStartPoint; 
    id <flDelegate> delegate; 

} 
@property CGPoint gestureStartPoint; 
@property (nonatomic,retain) id <flDelegate> delegate; 
@end 

@protocol flDelegate 

-(IBAction)flDidFinish:(fl *)controller; 

@end 

そして今:

#import "fl.h" 

@implementation fl 

@synthesize delegate; 
@synthesize gestureStartPoint; 


- (void)dealloc 
{ 
    [super dealloc]; 

} 

#pragma mark - View lifecycle 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch =[touches anyObject]; 
    gestureStartPoint =[touch locationInView:self.view]; 
} 

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch =[touches anyObject]; 
    CGPoint CurrentPosition =[touch locationInView:self.view]; 
    if(CurrentPosition.x >= kMinimumLength) 
    { 
     NSLog(@"Go"); 
    } 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

ここではすべてが働いているが、それは気の抜けたビールを検出していない、Goを印刷しません。

答えて

0

また、UISwipeGestureRecognizerを見てみることをお勧めしますが、現在のデザインにとどまることをお望みなら、これは他の場所でスワイプとピンチ(ズーム)を検出するコードです。 ケースに合わせる必要があります(3つのブランチの代わりに3つの方法があります)。

 if (touch.phase == UITouchPhaseBegan) { 
//   NSLog(@"TOUCH BEGAN"); 
      _initialView = touchView; 
      startTouchPosition1 = [touch locationInView:self]; 
      startTouchTime = touch.timestamp; 

      if ([allTouches count] > 1) { 
       startTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; 
       previousTouchPosition1 = startTouchPosition1; 
       previousTouchPosition2 = startTouchPosition2; 
      } 
     } 

     if (touch.phase == UITouchPhaseMoved) { 
//   NSLog(@"TOUCH MOVED"); 
      if ([allTouches count] > 1) { 
       CGPoint currentTouchPosition1 = [[[allTouches allObjects] objectAtIndex:0] locationInView:self]; 
       CGPoint currentTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; 

       CGFloat currentFingerDistance = CGPointDist(currentTouchPosition1, currentTouchPosition2); 
       CGFloat previousFingerDistance = CGPointDist(previousTouchPosition1, previousTouchPosition2); 
       if (fabs(currentFingerDistance - previousFingerDistance) > ZOOM_DRAG_MIN) { 
        NSNumber* movedDistance = [NSNumber numberWithFloat:currentFingerDistance - previousFingerDistance]; 
        if (currentFingerDistance > previousFingerDistance) { 
         NSLog(@"zoom in"); 
         [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_IN object:movedDistance]; 
        } else { 
         NSLog(@"zoom out"); 
         [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_OUT object:movedDistance]; 
        } 
       } 
      } 
     } 

     if (touch.phase == UITouchPhaseEnded) { 
      CGPoint currentTouchPosition = [touch locationInView:self]; 

      // Check if it's a swipe 
      if (fabsf(startTouchPosition1.x - currentTouchPosition.x) >= SWIPE_DRAG_HORIZ_MIN && 
//    fabsf(startTouchPosition1.y - currentTouchPosition.y) <= SWIPE_DRAG_VERT_MAX && 
       fabsf(startTouchPosition1.x - currentTouchPosition.x) > fabsf(startTouchPosition1.y - currentTouchPosition.y) && 
       touch.timestamp - startTouchTime < 0.7 
       ) { 
       // It appears to be a swipe. 
       if (startTouchPosition1.x < currentTouchPosition.x) { 
        NSLog(@"swipe right"); 
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_RIGHT object:touch]; 
       } else { 
        NSLog(@"swipe left"); 
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_LEFT object:touch]; 
       } 
      } 
      startTouchPosition1 = CGPointMake(-1, -1); 
      _initialView = nil; 
     } 

EDIT:あなたのコードについて...

あなたが適切なスワイプ検出を行っていません。実際には、次の行を使用してください:

if(CurrentPosition.x >= kMinimumLength) 

位置(CurrentPosition.x)と距離(kMinimumLength)を比較しています。これは本当に意味がありません。最後のタッチ位置を保存し、最後のタッチと現在のタッチの間の距離を計算し、この距離をkMinimumLenghtと比較する必要があります。

サンプルコードを投稿したのは、実際の実装を調べることで、スワイプを検出するために何を行う必要があるのか​​理解できるからです。

+0

私はあなたのコードをundersatndすることができますが、私のコードでエラーを見つけることはできません、あなたは私の上記のコードをチェックし、 –

0

あなたは間違いなくUISwipeGestureRecognizerを見てください。

+0

うわー!しかし、私は自分のコードでエラーが何であるか知りたいです –

関連する問題