2014-12-12 75 views
1

我已經創建圖像視圖通過這樣的代碼添加行動 -的編程方式創建的ImageView

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

    UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)]; 
    dot.image=[UIImage imageNamed:@"draw.png"]; 
    [self.view addSubview:dot]; 

} 

我想用戶交互添加到這個UIImageView,然後創建此UIImageView選擇或操作時拍了拍如何這樣做?

+0

使用UITapGestureRecognizer。 – rdelmar 2014-12-12 23:57:28

+0

您能否請您在回答中編寫代碼示例,我不知道如何分配UITapGestureRecognizer。 Objective-C – 2014-12-13 00:01:34

+0

非常新如果你是新手,那麼你應該閱讀文檔 - 在Xcode的文檔搜索中鍵入「手勢識別器」,然後這個文檔將解釋如何使用它們。 – rdelmar 2014-12-13 00:07:28

回答

0

這取決於你想要達到什麼樣的動作。一般來說你會使用UIGestureRecognizer。例如,如果您希望圖像響應輕擊手勢,那麼您將擁有類似以下內容的內容。
- (void)viewDidLoad { [super viewDidLoad]; //在加載視圖後執行任何其他設置,通常從筆尖 dotArray = [NSMutableArray alloc] init]; UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture :)];

for(int i = 0; i< 30; i++) { 
    UIImageView *dot =[[UIImageView alloc] ... 
    dot.image=[UIImage imageNamed:@"draw.png"]; 
    dot.tag = i; //identify dot image. 
    [self.view addSubview:dot]; 
    [dotArray addObject:dot]; 
    [dot addGestureRecognizer:tapGesture]; 
    ... 
    } 

    [tapGesture release]; 
} 

隨後的方法來處理雙擊手勢...

-(void)handleTapGesture:(id)sender { 
    UITapGestureRecognizer * tapGesture = (UITapGestureRecognizer*)sender; 
    for(int i = 0; i<[dotArray count]; i++) { 
    UIImageView * dot = (UIImageView*)[dotArray objectAtIndex:i]; 
    if(dot.tag == [tapGesture view].tag) { 
     //fade out animation 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.5f]; 
     dot.alpha = 0.0f; 
     [UIView commitAnimations]; 
} 

對於這個工作,你需要做點陣列,並宣佈它作爲實例變量,否則該方法不能訪問點。

+0

您能否解釋一下 - 當您處於HandleTapGesture中時,我想讓其中一個點消失,該怎麼做?因爲我使用代碼創建了30個點,所以當我點擊其中一個點時如何使點擊消失? – 2014-12-13 03:40:40

+0

我可能需要再次打勾。 – 2014-12-13 04:32:53

+0

加載我的模擬器.... – 2014-12-13 04:33:46

0

嘗試這樣的: -

UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)]; 
dot.image=[UIImage imageNamed:@"draw.png"]; 
[self.view addSubview:dot]; 

1)爲了實現所需的觸摸的數目是1指這樣的: -

UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)]; 
doubleTapRecognizer.numberOfTapsRequired = 2; 
doubleTapRecognizer.numberOfTouchesRequired = 1; 
[dot addGestureRecognizer:doubleTapRecognizer]; 

- (void)doubleTapped:(UITapGestureRecognizer*)recognizer 
{ 

} 

2)爲了實現所需的觸摸的數目爲2指這個: -

UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)]; 
twoFingerTapRecognizer.numberOfTapsRequired = 1; 
twoFingerTapRecognizer.numberOfTouchesRequired = 2; 
[dot addGestureRecognizer:twoFingerTapRecognizer]; 

- (void)twoFingerTapped:(UITapGestureRecognizer*)recognizer 
{ 

} 
+0

您能否解釋一下 - 當您處於HandleTapGesture中時,我想讓其中一個點消失,該怎麼做?因爲我使用代碼創建了30個點,所以當我點擊其中一個點時如何使點擊消失? – 2014-12-13 03:42:14

+0

1 - 是的,我在隨機位置產生了30個點,當點擊點時,我想'dot.hidden = YES',但我只需要點擊消失的點就可以了?你的答案更詳細,但兩個答案都忘了'dot.UserInteractionEnabled = YES;'你能編輯你的答案以適合我的需要,謝謝你提前。你對我的幫助很大 – 2014-12-13 03:54:25

+0

你能否提供一個編碼的例子?謝謝 – 2014-12-13 04:29:31

0

謝謝大家幫助我,我把每個人的答案放在一起,並把它作爲我的最終代碼。

// 
    // ViewController.m 
    // InvaderRush 
    // 
    // Created by Ajay Venkat on 13/12/2014. 
    // Copyright (c) 2014 AJTech. All rights reserved. 
    // 

    #import "ViewController.h" 

    @interface ViewController() 
    { 
     NSArray *dotArray; 

    } 
    @end 

    @implementation ViewController 
    - (void)viewDidLoad { 

     UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,100,100)]; 
     dot.image=[UIImage imageNamed:@"invader.jpg"]; 
     [self.view addSubview:dot]; 
     dot.tag = 1; //identify dot image. 


     UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapped:)]; 
     doubleTapRecognizer.numberOfTouchesRequired = 1; 
     [dot addGestureRecognizer:doubleTapRecognizer]; 
      dot.userInteractionEnabled = YES; 
     NSMutableArray *images =[[NSMutableArray alloc] initWithObjects: dot,nil]; 


    } 

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

    -(void)doubleTapped:(id)sender { 
     UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender; 
     UIImageView *imageView = (UIImageView *)recognizer.view; 

     if(imageView.tag==1) { 
      [imageView setImage:[UIImage imageNamed:@"space_invader.jpg"]]; 
     } 
    } 

    -(void)handleTapGesture:(id)sender { 
     } 
    @end