我首先说我对于ObjectiveC是全新的,并且也是第一次使用界面生成器。 我只是想让平底锅的手势工作(我将需要变焦手势也为我真正想做的事情,但我认为如果我从容易的平底锅开始)。 我跟着一些教程,但它仍然无法在我的iPhone 5和模拟器上工作。 我已经开始基于页面的应用程序的东西。然后,我为那个人建立了一个新的视图和一个视图控制器。然后,我将“界面”构建器中的Pan Gesture拖放到视图中。视图上的用户交互已启用。iOS界面识别器通过界面生成器添加
这里是我的代码:
uACropViewController.h
#import <UIKit/UIKit.h>
@interface uACropViewController : UIViewController
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;
@property (weak, nonatomic) IBOutlet UIImageView *CropImage;
@property (strong, nonatomic) IBOutlet UIPanGestureRecognizer *onGestureAction;
@end
uACropViewController.m
#import "uACropViewController.h"
@interface uACropViewController()
@end
@implementation uACropViewController
@synthesize CropImage;
static CGPoint originalPoint;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//for resetting the center of the image, when the pan gesture is ended.
originalPoint=CropImage.center;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)onGestureAction:(id)sender{
if ([sender isKindOfClass:[UIPanGestureRecognizer class]]) {
//is a pan gesture
UIPanGestureRecognizer *recognizer=(UIPanGestureRecognizer *)sender;
if (recognizer.state==UIGestureRecognizerStateBegan||recognizer.state==UIGestureRecognizerStateChanged) {
CGPoint nextPoint= [recognizer translationInView:self.view];
CGPoint currentPoint=CropImage.center;
currentPoint.x +=nextPoint.x;
currentPoint.y +=nextPoint.y;
CropImage.center=currentPoint;
[recognizer setTranslation:CGPointZero inView:self.view];
}
if (recognizer.state==UIGestureRecognizerStateEnded) {
CropImage.center=originalPoint;
[recognizer setTranslation:CGPointZero inView:self.view];
}
return;
}
}
@end
说实话,我没有什么错在这里的想法。所以任何帮助或指针表示赞赏! 谢谢, Suse
为什么你使用'onGestureAction:'和'不handlePan:'? –
你试过我的代码吗? –
@guy Kogus:如果我改变onGestureAction到handlePan不会改变任何东西...... antionio毫克,见下文,但你的代码还没有做它为我工作... – suMi