2012-01-04 61 views
1

我试着从下面的文章得到同步平移和捏,但是我不认为委托因为即时通讯看到没有影响正常勾搭上了。现在我有一个锅和一个捏手势识别,无论工作。我做IBOutlets我的ViewController,在我的viewController初始化有:实施UIGestureRecognizerDelegate

编辑

这里是标头和实现文件分别。

头:

#import <UIKit/UIKit.h> 
#import <OpenGLES/EAGL.h> 
#import <OpenGLES/ES1/gl.h> 
#import <OpenGLES/ES1/glext.h> 

@class GLView; 
@interface GLViewController : UIViewController <UIGestureRecognizerDelegate> 

@property (strong, nonatomic) IBOutlet UIPinchGestureRecognizer *pinchRecognizer; 
@property (strong, nonatomic) IBOutlet UIPanGestureRecognizer *panRecognizer; 

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer; 
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer; 
- (void)drawView:(GLView*)view; 
- (void)setupView:(GLView*)view; 

@property (retain, nonatomic) IBOutlet UILabel *zoomFactorLabel; 
@property (retain, nonatomic) IBOutlet UILabel *xPosLabel; 
@property (retain, nonatomic) IBOutlet UILabel *yPosLabel; 

@end 

    panRecognizer.minimumNumberOfTouches = 1; 
    panRecognizer.delegate = self; // Very important 
    pinchRecognizer.delegate = self; // Very important 

实现:

#import "GLViewController.h" 
#import "GLView.h" 
#import "OpenGLCommon.h" 
#import "ConstantsAndMacros.h" 
#import "TileManager.h" 
#import <CoreGraphics/CoreGraphics.h> 

@implementation GLViewController 
@synthesize pinchRecognizer; 
@synthesize panRecognizer; 

@synthesize zoomFactorLabel; 
@synthesize xPosLabel; 
@synthesize yPosLabel; 
TileManager* tileManager; 
CGPoint currentPosition; 
CGPoint start; 
CGFloat temp = 0; 
CGFloat x=0; 
CGFloat y=0; 
CGFloat mLastScale=1; 
CGFloat mCurrentScale=1; 
CGPoint translation; 
- (id) init { 
    self = [super init]; 
    if (self != nil) { 
     printf("GLViewController initialized."); 
     tileManager=[[TileManager alloc] init]; 
     currentPosition = CGPointMake(0, 0); 
    } 
    return self; 
} 
CGFloat scale=1; 
CGPoint position; 
CGPoint mid; 


#pragma mark - UIGestureRecognizerDelegate 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    NSLog(@"delegate called"); 

      // If you have multiple gesture recognizers in this delegate, you can filter them by comparing the gestureRecognizer parameter to your saved objects 
    return YES; // Also, very important. 
} 

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer { 
    scale=scale*recognizer.scale; 
    mCurrentScale += [recognizer scale] - mLastScale; 
    mLastScale = [recognizer scale]; 
    if (recognizer.state == UIGestureRecognizerStateBegan) 
    { 
     //get midpoint 
     CGPoint zero=[recognizer locationOfTouch:0 inView:self.view]; 
     CGPoint one=[recognizer locationOfTouch:1 inView:self.view]; 
     float x=zero.x+one.x; 
     float y=zero.y+one.y; 
     mid.x=x/2; 
     mid.y=y/2; 
    } 
    else if (recognizer.state == UIGestureRecognizerStateEnded) 
    { 
     mLastScale = 1.0; 
    }  

NSString *xPosString = [NSString stringWithFormat:@"%.2f",mid.x]; 
    NSString *yPosString = [NSString stringWithFormat:@"%.2f",mid.y]; 
    xPosLabel.text=xPosString; 
    yPosLabel.text=yPosString; 
} 

- (IBAction)handlePan:(UIPanGestureRecognizer*)recognizer {  
    [recognizer setMaximumNumberOfTouches:1]; 
    translation= [recognizer translationInView:self.view]; 
    NSString *xPosString = [NSString stringWithFormat:@"%.2f",currentPosition.x]; 
    NSString *yPosString = [NSString stringWithFormat:@"%.2f",currentPosition.y]; 
    xPosLabel.text=xPosString; 
    yPosLabel.text=yPosString; 
    currentPosition.x=currentPosition.x+translation.x; 
    currentPosition.y=currentPosition.y+translation.y; 
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; 
    } 


- (void)drawView:(GLView*)view 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glLoadIdentity(); 
    glTranslatef(mid.x, -mid.y, 0); 
    glScalef(mCurrentScale,mCurrentScale, 0); 
    glTranslatef(-mid.x, mid.y, 0); 
    glTranslatef(currentPosition.x,currentPosition.y*-1,0); 
    [tileManager drawView:view]; 
    //draw calls 
} 
-(void)setupView:(GLView*)view 
{ 
    CGRect rect = view.bounds; 
    glViewport(0, 0,rect.size.width,rect.size.height); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrthof(0,(rect.size.width),-(rect.size.height),0, -1, 1) ; 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();  

    glClearColor(0,1,1, 1); 
    // Enable Smooth Shading, default not really needed. 
    glShadeModel(GL_SMOOTH); 
    // Depth buffer setup. 
    glClearDepthf(1.0f); 


    //enable textures. 
    glEnable(GL_TEXTURE_2D); 
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

    [tileManager setupView:view];  
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
    panRecognizer.delegate=self; 
    pinchRecognizer.delegate=self; 
NSLog(@"pan recognizer delegate [%@]", [panRecognizer.delegate description]); 
} 

- (void)viewDidUnload { 
    [self setPinchRecognizer:nil]; 
    [self setPanRecognizer:nil]; 
    [super viewDidUnload]; 
} 
@end 

一个的NSLog是在委托方法的身体,但永远不会被执行。任何帮助表示赞赏。

article

+0

。 – DBD 2012-01-04 21:13:08

+0

是在委托方法签名缺少参数之间的空间中的一个错字或在你的实际代码? – 2012-01-04 21:21:18

+0

马克·亚当斯:哇,原来如此。邮报说,作为一个接受并给予好评 – jfisk 2012-01-04 21:24:11

回答

-2

你可能抓住它现在 - 但似乎你错过在委托的空间:我认为,我们需要看到更多的诊断问题

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer 
0

如果视图控制器从界面生成器加载,则初始化是错误的地方设置的东西了。在-viewDidLoad中执行。

+0

这就是设置,并且我有验证GLViewController是其委托日志条目,但它永远不会调用shouldRecognizeSimultaneouslyWithGestureRecognizer答案:方法 – jfisk 2012-01-04 21:15:00