2011-05-20 35 views
3

嘿傢伙, 我現在只是在這裏難住,我希望在手指按下時創建一個UIView動畫,通過在手指放置位置創建一個UIView。這可能嗎?iPhone - 在觸摸位置使用UIView動畫

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch * touch = [touches anyObject]; 
    CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow]; 
    NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y); 
    //CGRect touchFrame = CGRectMake(pos.x, pos.y, 100, 100); 
    UIView *box = [[UIView alloc] initWithFrame:CGRectMake(pos.x, pos.y, 100, 100)]; 
    NSLog(@"%f", box.frame.origin.x); 
    [self.view addSubview:box]; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    [UIView setAnimationTransition:110 forView:box cache:NO]; 
    [UIView commitAnimations]; 
    [box removeFromSuperview]; 
    [box release]; 
} 

任何建議都非常歡迎。

+0

這是可能的,但到底是什麼問題? – Joze 2011-05-20 09:16:47

+0

哎呀,對不起!這裏有點累了。運行時,不會對設置框區域產生影響。 – 2011-05-20 09:20:50

+0

這應該通過設置視圖的邊界來解決,而不是像你這樣做。請參閱下面的答案。 – sergio 2011-05-20 09:40:21

回答

3

其因到u書面以下行

[box removeFromSuperview]; 
    [box release]; 

此行u有框圖像動畫完成後調用。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch * touch = [touches anyObject]; 

    CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow]; 

    UIView *box = [[UIView alloc] initWithFrame:CGRectMake(pos.x, pos.y, 100, 100)]; 

    [self.view addSubview:box]; 

    [UIView beginAnimations:nil context:nil]; 

    [UIView setAnimationDuration:1.0]; 

    [UIView setAnimationTransition:110 forView:box cache:NO]; 

    [UIView commitAnimations]; 

[self performSelector:@selector(releaseBoxImge:) withObject:box afterDelay:1.0]; 

} 


-(void)releaseBoxImge:(UIView *)boxImage 
{ 

[boxImage removeFromSuperview]; 

    [boxImage release]; 

} 
+0

這並沒有幫助,雖然我看到你要去哪裏。它不一定是[[自我盒子] removeFromSuperview]?因爲它是在TouchesBegan中創建和分配的。如果我只是分配/初始化它在頭文件中,我可以只是做一些像 box.frame = CGRectMake(pos.x,pos.y,100,100); ? – 2011-05-20 09:28:15

+0

'-releaseBoxImge'函數不會編譯... – sergio 2011-05-20 09:28:46

+0

對不起,我忘了將你的盒子圖像傳遞給選擇器method.Above編輯的代碼現在肯定會工作。 – Tirth 2011-05-20 09:33:20

0

像無線電規則委員會說,做removeFromSuperView動畫完成後(我不知道他肯定會的代碼)。它應該看起來像這樣,我認爲:

//initializations of everything here .. 
[UIView animateWithDuration:1.0     
animations:^ 
{ 
    //do animations here 
} 
completion:^(BOOL finished) 
{ 
    //do clean up here 
}];