2013-04-25 66 views
1

我是Objective-C的新手,嘗試製作一個簡單的應用程序,當您觸摸對象時,它將隨機移動一段時間並停止。那麼你需要再次觸摸它,這樣它會再次移動並在一段時間後停止。如何通過在iOS中觸摸對象來移動對象

我已經搜索了觸摸方法和一些教程,但問題是我不知道如何開始。這就像我需要一個移動對象和一個觸摸對象的函數,但我不知道如何連接它們並使用它們。

這裏是一個教程,它幫助我瞭解了很多功能,實際上它的功能與我的應用程序相反。但我仍然無法自己開始編程。

http://xcodenoobies.blogspot.se/2010/11/under-construction.html

任何幫助,將不勝感激,關於如何我的邏輯和如何找到正確的方法,以及如何找到我需要的變量類型的節目。

+1

你有什麼試圖改變上述項目以適應你的需求? – 2013-04-25 08:35:02

+0

你應該看看IBOutlets如何工作,以及如何響應觸摸輸入來移動一個對象,你只需要改變它的框架(或在超級視圖中的位置),如果你想讓動畫考慮一個動畫塊。 你可以在互聯網上找到關於這些主題的一切。 – Maurice 2013-04-25 08:43:59

回答

1

你需要的是一個手勢識別器添加到您希望能夠觸摸的觀點:

// In a view controller or in a UIView subclass 
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
[self addGestureRecognizer:tapGestureRecognizer]; 
// Or [self.view addGestureRecognizer:tapGestureRecognizer]; 

- (void)handleTap:(UITapGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateEnded) { 
     // Animate the view, move it around for a while etc. 
     // For animating a view use animateWithDuration:animations: 
    } 
} 
1

其實蘋果做了一個演示這一點。

http://developer.apple.com/library/ios/#samplecode/MoveMe/Introduction/Intro.html

你可以嘗試修改這個代碼,您的需求。而實際的功能您要尋找的地方:

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 

如果這是您要尋找的請點擊答案「回答」,所以這個問題可以作爲:-)關閉考慮。

1

如果我正確地得到你,我會說最簡單的方法來實現你所要求的是在界面生成器中創建一個UIButton並將它連接到一個IBAction,它將它移動到一個隨機點。然後,您可以添加一個自定義圖形的按鈕..

  1. 創建您的視圖控制器的公共方法,返回類型IBAction爲
  2. 創建IB一個按鈕,它的「潤色Inside」的插座連接到您的IBAction爲
  3. 我你的IBAction爲方法,在屏幕邊界內生成一個隨機的x和y座標,併爲此移動動畫。

我會/不會詳細討論特定的代碼,因爲它會佔用很多空間。 請注意,你的問題是非常開放和模糊的,這在StackOverflow上不被認爲是很好的風格。此外,您可能wan't節省如動畫的東西,直到你多一點經驗與iOS和Objective-C

-V

0
  • (無效)的touchesBegan:(NSSet中*)觸及withEvent :(UIEvent *)事件;

是用戶觸摸視圖時調用的方法。如果你在主視圖(大視圖)中覆蓋它,你將不得不尋找接觸點是否是你的鏈接中描述的輔助方法的對象。否則你可以重載你的對象的類&實現該方法,所以你不必明確地發現,如果接觸點是在對象上或沒有。

爲您的要求。我會說覆蓋裏面的uiimageview &,把touchesbegan實施它會正常工作。

.h file 

@interface StarView : UIImageView 

@end 

.m file 

@implementation StarView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // your code in the link with the related methods 

Destination = CGPointMake(arc4random() % 320, arc4random() % 480); 

// calculate steps based on speed specified and distance between the current location of the sprite and the new destination 

xamt = ((Destination.x - self.center.x)/speed); 

yamt = ((Destination.y - self.center.y)/speed); 

// ask timer to execute moveBall repeatedly each 0.2 seconds. Execute the timer. 

mainTimer = [NSTimer scheduledTimerWithTimeInterval:(0.02) target:self selector:@selector(moveBall) userInfo:nil repeats: NO]; 
} 

不要忘記在此之後複製moveBall方法。

在你MAINVIEW只是讓升景&的一個實例添加到MAINVIEW

2

步驟1:將這個代碼在你ViewDidLoad方法,其中,我已經創造了一些UIImageView並將其添加到隨機查看

[self.view setTag:1]; 
for(int i=0;i<4;i++) 
{ 
    int x = arc4random()%300; 
    int y = arc4random()%400; 

#warning set Image here 

    UIImageView *imgview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage.png"]]; 
    [imgview setFrame:CGRectMake(x, y, 25, 25)]; 
    [imgview setUserInteractionEnabled:YES]; 
    [self.view addSubview:imgview]; 
} 

第2步:定義touchBegan方法來處理觸摸和移動視圖周圍的對象,我們已設置標記= 1爲ViewController,因爲我們不想移動我們的主視圖,只有子視圖將被移動

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    if([[touch view] tag] != 1) 
    { 
     [UIView animateWithDuration:0.25f animations:^{ 
      int x = arc4random()%300; 
      int y = arc4random()%400; 

      [[touch view] setCenter:CGPointMake(x, y)]; 
     }]; 
    } 
} 
+0

感謝Buddy,它幫助我獲得了不同的邏輯。 – 2014-05-15 04:07:27