2010-06-03 123 views
1

是否有人可以告訴我如何在用戶點擊屏幕並使其出現在水龍頭位置時顯示圖像。 由於提前, 泰特使圖像顯示...如何?

+0

請不要再發問。你可以編輯你現有的一個,將它撞到積極的哨兵頂部,並可能得到更多的關注,並且(一旦你積累了一些代表),你可以提供賞金。我要標記這個作爲合併候選人的版主,但爲了將來參考**不要這樣做!** – dmckee 2010-09-29 22:13:17

+0

是的,請不要發佈重複。我把你的兩個問題合併在一起。無論您擁有多少名譽,您都可以隨時編輯自己的帖子。您也可以在自己的問題上留下意見並回答您的問題,因此請在適當的地方使用意見而不是答案。有關更多詳細信息,請參見[常見問題](http://stackoverflow.com/faq)。 – 2010-09-30 01:12:23

回答

2

UIViewUIResponder子類,它具有以下方法可能會有所幫助:-touchesBegan:withEvent:-touchesEnded:withEvent:-touchesCancelled:withEvent:-touchesMoved:withEvent:

其中每個對象的第一個參數是NSSetUITouch對象。 UITouch有一個-locationInView:實例方法,它應該在您的視圖中產生水龍頭的位置。

0

從問題中可以看出,您希望用戶能夠點擊屏幕上的任意位置,並在他們點擊的位置繪製圖像?而不是在指定的地方進行拍照並讓圖像出現在那裏?

如果是這樣,你可能將不得不去與自定義視圖。在這種情況下,您可以執行以下操作:

  1. 創建一個UIView的子類。
  2. 覆蓋touchesBegan方法。請致電[[touches anyObject] locationInView:self](其中touches是該方法的第一個參數,的UITouch對象)以獲取觸摸的位置並進行記錄。
  3. 重寫touchesEnded方法。使用與步驟2中相同的方法確定位置觸摸已結束。
  4. 如果第二個位置靠近第一個位置,則需要將圖像放置在該位置。記錄該位置並致電[self setNeedsDisplay]以導致自定義視圖重新繪製。
  5. 重寫drawRect方法。這裏,如果在步驟4中設置了位置,則可以使用UIImage方法drawAtPoint在選定位置繪製圖像。

For further details, this link might be worth a look。希望有所幫助!

編輯:我注意到你之前已經問過基本相同的問題。如果你對那裏給出的答案不滿意,通常認爲可以更好地「碰撞」舊的問題,或許編輯它以要求進一步澄清,而不是創建一個新問題。

編輯:根據要求,一些非常簡短的示例代碼如下。這可能不是最好的代碼,我還沒有測試過,所以它可能會有點小。爲了說明起見,THRESHOLD允許用戶輕敲一下手指(最多3px),因爲在不移動手指的情況下點擊非常困難。

MyView.h

#define THRESHOLD 3*3 

@interface MyView : UIView 
{ 
    CGPoint touchPoint; 
    CGPoint drawPoint; 
    UIImage theImage; 
} 

@end 

MyView的。m

@implementation MyView 

- (id) initWithFrame:(CGRect) newFrame 
{ 
    if (self = [super initWithFrame:newFrame]) 
    { 
     touchPoint = CGPointZero; 
     drawPoint = CGPointMake(-1, -1); 
     theImage = [[UIImage imageNamed:@"myImage.png"] retain]; 
    } 

    return self; 
} 

- (void) dealloc 
{ 
    [theImage release]; 
    [super dealloc]; 
} 

- (void) drawRect:(CGRect) rect 
{ 
    if (drawPoint.x > -1 && drawPoint.y > -1) 
     [theImage drawAtPoint:drawPoint]; 
} 

- (void) touchesBegan:(NSSet*) touches withEvent:(UIEvent*) event 
{ 
    touchPoint = [[touches anyObject] locationInView:self]; 
} 

- (void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event 
{ 
    CGPoint point = [[touches anyObject] locationInView:self]; 
    CGFloat dx = point.x - touchPoint.x, dy = point.y - touchPoint.y; 

    if (dx + dy < THRESHOLD) 
    { 
     drawPoint = point; 
     [self setNeedsDisplay]; 
    } 
} 

@end 
1

您可以創建初始星形,並在每次觸摸視圖時移動它。 我不確定你最終的結果會是什麼樣子。

注: 此代碼會給你1星級,與自來水 這裏移動是我的代碼: -

(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSSet *allTouches = [event allTouches]; 
    switch ([allTouches count]) { 
     case 1: 
     { 
      UITouch *touch = [[allTouches allObjects] objectAtIndex:0]; 
      CGPoint point = [touch locationInView:myView]; 
      myStar.center = point; 
      break; 
     } 
     default: 
      break; 
    } 
}