我试图同时从下面的文章中获取平移和捏但我不认为代表被正确连接,因为我看不到任何效果。现在我有一个平底锅和一个捏手势识别器,两者都有效。我将IBOutlets设置为我的viewcontroller,并在我的viewController初始化程序中有:
这里分别是标题和实现文件。
头:
#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位于委托方法的主体中,但永远不会被执行。任何帮助表示赞赏。
答案 0 :(得分:0)
如果从界面构建器加载了视图控制器,则初始化程序是设置该东西的错误位置。在-viewDidLoad中执行。
答案 1 :(得分:-2)
你现在可能已经抓住它 - 但似乎你错过了代表中的一个空格:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer
到
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer