2013-10-14 114 views
0

我首先說我對於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

+0

爲什麼你使用'onGestureAction:'和'不handlePan:'? –

+0

你試過我的代碼嗎? –

+0

@guy Kogus:如果我改變onGestureAction到handlePan不會改變任何東西...... antionio毫克,見下文,但你的代碼還沒有做它爲我工作... – suMi

回答

0

IBOutlet是該視圖中該對象的引用,IBAction是在該變量上觸發事件時調用的操作。

例如,您可以使用UIButton IBOutlet來更改UIButton的屬性(如大小),以及UIButton IBAction來識別何時按下該按鈕。

在你的情況,onGestureAction是指向該手勢識別一個變量,hanldePan:是的動作響應。

嘗試增加這裏的方法:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer 
{ 
      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; 
     } 
+0

我嘗試過,但它仍然沒有按」不要做任何事情......也許我在不同的位置做錯了事情?真的不知道... – suMi