实现 UIGestureRecognizerDelegate

发布于 2024-12-24 03:33:35 字数 5368 浏览 2 评论 0原文

我试图从下面的文章中同时进行平移和捏合,但我认为代表没有正确连接,因为我没有看到任何效果。现在我有一个平移和捏合手势识别器,两者都可以工作。我将 IBOutlet 添加到我的视图控制器中,并在我的视图控制器初始值设定项中具有:

编辑

此处分别是标头文件和实现文件。

header:

#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 位于委托方法的主体中,但从未被执行。任何帮助表示赞赏。

文章

Im trying to get simultaneous pan and pinch from the article below but i dont think the delegate is hooked up properly since im seeing no effect. Right now I have a pan and a pinch gesture recognizer that both work. I make IBOutlets to my viewcontroller, and in my viewController initializer have:

EDIT

here is the header and implementation files respectively.

header:

#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

implementation:

#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

a NSLog is in the body of the delegate method but never gets executed. Any help is appreciated.

article

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼疼热情 2024-12-31 03:33:35

如果视图控制器是从界面构建器加载的,那么初始化程序就不是设置这些东西的地方。在-viewDidLoad 中执行此操作。

If the view controller is loaded from the interface builder, then the initializer is the wrong place to set that stuff up. Do it in -viewDidLoad.

混浊又暗下来 2024-12-31 03:33:35

你现在可能已经明白了 - 但似乎你错过了委托中的一个空格

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer

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

you probably caught it by now - but it seems that you miss a space in the delegate:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer

to

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文