2011-07-06 10 views
1

저는 iOS 개발에 상당히 익숙하며 앱 개발에 열심히 노력하고 있습니다. 멀티 뷰 앱에서 스 와이프 제스처를 사용할 수 없습니다. 코드를 붙여 넣고 있습니다. 오류를 지적하십시오. 보기 기반 응용 프로그램입니다. Multiview App에서 스 와이프 제스처

여기
#import <UIKit/UIKit.h> 

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

mainViewController.h

지금 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에서

:

#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 

그리고 지금 fl.m에서 :

#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; 
} 

여기에 모든 노력하지만, 와이프를 감지되지 않으며, 이동을 인쇄 할 수 없습니다.

답변

0

UISwipeGestureRecognizer를 살펴 보는 것도 좋지만, 현재 디자인을 유지하기를 원한다면 다른 곳에서 사용하여 코드를 감지하고 집어 넣을 수 있습니다 (확대/축소). 사례에 맞게 조정해야합니다 (세 가지 대신 세 가지 방법이 있습니다).

 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; 
     } 

편집 : 코드 ...

에 대해 당신은 적절한 슬쩍 감지를하고 있지 않습니다.

if(CurrentPosition.x >= kMinimumLength) 

당신이 거리 (kMinimumLength)와 위치 (CurrentPosition.x)을 비교하는 : 사실,이 라인을. 이것은 실제로 의미가 없습니다. 마지막 터치 위치를 저장 한 다음 마지막 터치와 현재 터치 사이의 거리를 계산하고이 거리를 kMinimumLenght와 비교하는 것이 필요합니다.

샘플 코드를 게시 한 이유는 작동중인 구현을 검사하여 스 와이프를 감지하기 위해 수행해야 할 작업을 이해할 수 있기 때문입니다.

+0

코드를 undersatnd 수 있지만 내 코드에서 오류를 찾을 수 없습니다, 위의 코드를 확인하고 오류를 가리 킵니다. –

관련 문제