2014-10-04 32 views
0

我正在嘗試創建一個iPad應用程序,用戶可以在其中創建狀態模型來描述離子通道的動態屬性。我需要完成的第一個目標是允許用戶創建任意數量的打開或關閉狀態,每個狀態都由UILabel對象表示。理想情況下,我希望允許用戶在創建後自由移動任何標籤。在試圖實現這個設計時,我遇到了一些問題。我搜索了所有類似的問題,但無法找到我正在尋找的東西。如何創建任意數量的UILabel對象,每個對象都有一個PanGestureRecognizer。

我創建了兩個UIButtons,一個創建一個名爲openState的新的綠色UILabel,另一個創建一個名爲closedState的新的紅色UILabel。在通過單擊相應按鈕創建這些狀態之後,平移手勢識別器被添加到指定動作的標籤,移動,這將允許用戶在他們希望的視圖中的任何位置移動標籤。

這適用於如果我創建一個標籤。我可以自由移動它。但是,如果我在此之後創建另一個,新的可以自由移動,但前一個失去了這種能力。我怎樣才能爲每個永久留下的標籤創建手勢識別器?我想我應該讓一個NSMutableDictionary來保存所有不同的手勢識別器,並給它們不同的鍵名稱。我現在認識到,以前狀態的手勢識別器失敗,因爲每次調用createNewClosedState或createNewOpenState時,它都會以相同名稱_panGestureRecognizer進行聲明。

我已經發布了下面的整個源代碼。許多註釋過的行是我以前在解決問題時所做的嘗試。

如果有人能夠解決這個問題,我將不勝感激。我對Objective-C和一般編程非常陌生。這是我的第一個大項目,我相信我會因爲對某些概念的誤解而造成一些容易的錯誤。

謝謝!

****ViewController.h**** 
#import <UIKit/UIKit.h> 


@interface ViewController : UIViewController<UIGestureRecognizerDelegate> 


@property (strong, nonatomic) IBOutlet UIButton *createClosedStateButton; 
@property (strong, nonatomic) IBOutlet UIButton *createOpenStateButton; 



//@property (strong, nonatomic) IBOutlet UIView *stateLabel; 

@property (strong, nonatomic) UIPanGestureRecognizer *panGestureRec; 



@end 



**ViewController.m** 
#import "ViewController.h" 


@interface ViewController() 
{ 
    int stateCount; 
    int numClosedStates; 
    int numOpenStates; 

    NSMutableDictionary *stateDictionary; 
    NSMutableDictionary *gestureRecognizers; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    stateCount = 0; 
    numClosedStates = 0; 
    numOpenStates = 0; 

    stateDictionary = [[NSMutableDictionary alloc] init]; 
    gestureRecognizers = [[NSMutableDictionary alloc] init]; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(IBAction)createNewClosedState:(id)sender 
{ 
    NSLog(@"Closed State Button was clicked."); 

    //Increase count of closed states 
    numClosedStates++; 


    //Create new label object for the new closed state. 
    UIView *stateLabel = [[UIView alloc] initWithFrame:CGRectMake(self.createClosedStateButton.frame.origin.x, self.createClosedStateButton.frame.origin.y + 100, 50, 50)]; 

    _panGestureRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move: forView:)]; 



    //_stateLabel.text = [NSString stringWithFormat:@"C%i", numClosedStates]; 

    //center text and set background color. 
    // _stateLabel.textAlignment = NSTextAlignmentCenter; 
    stateLabel.backgroundColor = [UIColor redColor]; 

    //Add gesture recognizer to the state label. 
    [stateLabel addGestureRecognizer:_panGestureRec]; 

    //Add state view to stateDictionary. 
    //NSString *stateIdentifier = [NSString stringWithFormat:@"Closed State %i", numClosedStates]; 
    //[stateDictionary setObject:stateLabel forKey:stateIdentifier]; 
    //[gestureRecognizers setObject:_panGestureRec forKey:stateIdentifier]; 

    //add label to view. 
    [self.view addSubview:stateLabel]; 


    //Increase total state count by one. 
    stateCount++; 
    NSLog(@"State count is %i", stateCount); 
} 

-(IBAction)createNewOpenState:(id)sender 
{ 
    NSLog(@"Open State Button was clicked."); 

    //Increase amount of open states. 
    numOpenStates++; 

    //Create new label object for new open state. 
    UIView *stateLabel = [[UIView alloc] initWithFrame:CGRectMake(self.createClosedStateButton.frame.origin.x, self.createClosedStateButton.frame.origin.y + 100, 50, 50)]; 


    //_stateLabel.text = [NSString stringWithFormat:@"O%i", numOpenStates]; 

    _panGestureRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move: forView:)]; 



    //center text and set background color. 
    //_stateLabel.textAlignment = NSTextAlignmentCenter 
    stateLabel.backgroundColor = [UIColor greenColor]; 

    //Add gesture recognizer to the state label. 
    [stateLabel addGestureRecognizer:_panGestureRec]; 

    //Add state view to stateDictionary. 
    //NSString *stateIdentifier = [NSString stringWithFormat:@"Open State %i", numOpenStates]; 
    //[stateDictionary setObject:stateLabel forKey:stateIdentifier]; 
    //[gestureRecognizers setObject:_panGestureRec forKey:stateIdentifier]; 

    //add label to view. 
    [self.view addSubview:stateLabel]; 


    //Increase total state count by one. 
    stateCount++; 
    NSLog(@"State count is %i", stateCount); 

} 

-(void)move:(id)sender forView:(UIView *)stateLabel 
{ 


    if (_panGestureRec.state == UIGestureRecognizerStateBegan) 
    { 
     // Start of the gesture. 
     // You could remove any layout constraints that interfere 
     // with changing of the position of the content view. 
    } 
    else if (_panGestureRec.state == UIGestureRecognizerStateChanged) 
    { 
     // Calculate new center of the view based on the gesture recognizer's 
     // translation. 
     CGPoint newCenter = stateLabel.center; 
     newCenter.x += [_panGestureRec translationInView:self.view].x; 
     newCenter.y += [_panGestureRec translationInView:self.view].y; 

     // Set the new center of the view. 
     stateLabel.center = newCenter; 

     // Reset the translation of the recognizer. 
     [_panGestureRec setTranslation:CGPointZero inView:self.view]; 
    } 
    else if (_panGestureRec.state == UIGestureRecognizerStateEnded) 
    { 
     // Dragging has ended. 
     // You could add layout constraints back to the content view here. 
    } 
} 

@end 
+0

如果你把這個作爲一個項目,我可以下載並立即運行,這將使它更容易幫助。 – algal 2014-10-04 00:30:04

回答

0

我認爲問題出在您的操作方法上。平移手勢識別器的操作方法應該只有一個參數,它是識別器本身(應該鍵入UIPanGestureRecognizer)。您可以通過sender.view獲取它所附帶的視圖。你不需要保存識別器的字典,因爲每個視圖都有它自己的。

+0

謝謝,問題在於操作方法。將其修改爲只有識別器作爲參數的IBAction方法。謝謝你的幫助! – 2014-10-07 20:14:54

相關問題