2012-04-18 113 views
1

嗨,我是iPhone開發新手。請任何機構告訴我,如果我有兩個意見一個另一個,我想,當我點擊超級查看它會拖動,當我點擊子視圖時,它會拖動。有誰能夠幫助我?如何將觸摸移動控件從一個uiview移動到另一個移動iPhone上的方法

這裏是我的代碼:

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *aTouch = [touches anyObject]; 
    //UITouch *aTouchSuper = [touches anyObject]; 
    CGPoint location = [aTouch locationInView:self.view]; 

    //CGPoint locationSuper=[aTouchSuper locationInView:viewForTouch]; 


    [UIView beginAnimations:@"Dragging A DraggableView" context:nil]; 




    //////////////////////// 
    //if(location.x>50 &&location.x<300){ 
//  viewForTouch.frame = CGRectMake(location.x,0, 
//          viewForTouch.frame.size.width, viewForTouch.frame.size.height); 
//  
// 
// 
// } 
    UIView *subview = [self.view hitTest:[[touches anyObject] locationInView:self.view] withEvent:nil]; 

    NSLog(@"%i",subview.tag); 
    if (location.x <= 50) 
    { 
     location.x = 50; 
    } 
    if (location.x >= 300) 
    { 
     location.x = 300; 
    } 
    if(subview.tag==12){ 

     viewForTouchInner.frame = CGRectMake(location.x,0, 

              viewForTouchInner.frame.size.width, viewForTouchInner.frame.size.height); 
    } 
    if(subview.tag==11) 
    { 
     viewForTouch.frame = CGRectMake(location.x,0, 
             viewForTouch.frame.size.width, viewForTouch.frame.size.height); 
    } 



    /////////////////////// 
    //if(viewForTouchInner.hidden=FALSE){ 
// 
//  
//  
//  
//  location.x=0; 
//  NSLog(@"%f",location.x); 
//  viewForTouchInner.frame = CGRectMake(location.x,0, 
//   
//          viewForTouch.frame.size.width, viewForTouch.frame.size.height); 
//  
// 
// } 
     [UIView commitAnimations]; 
    [buttonForMove addTarget:self action:@selector(Show) forControlEvents:UIControlEventTouchUpInside]; 
} 
+0

但你爲什麼使用 - (void)touchesMoved:(NSSet *)觸及事件:(UIEvent *)事件{}。此方法用於從圖像中獲取觸摸事件。從自定義類選項卡更改您的視圖爲UIcontroll比它會得到觸摸事件。 – freelancer 2012-04-18 06:41:16

回答

0

這裏是我的代碼在IOS拖累上海華視圖。 假設我有一個按鈕subview.when這個按鈕點擊這個動作called.It檢查是否subview被放置在x == 0它動畫它x = 260,反之亦然 注意你也必須添加框架QuartzCore/QuartzCore和#進口QuartzCore/QuartzCore.h在拖動 你action.setAnimationDuration節目持續時間的.m文件 - (IBAction爲)listButtonTapped {

// [self.navigationController popViewControllerAnimated:YES]; 
    //[self.profileTableView reloadData]; 
    NSLog(@"ButtonTapped"); 
    if(profileView.frame.origin.x == 0){ 
     [UIView beginAnimations:@"advancedAnimations" context:nil]; 
     [UIView setAnimationDuration:3.0]; 
     CGRect aFrame = profileView.frame; 
     aFrame.origin.x = 260; 
     profileView.frame = aFrame; 

     [UIView commitAnimations]; 
    } 
    else{ 

     [UIView beginAnimations:@"advancedAnimations" context:nil]; 
     [UIView setAnimationDuration:3.0]; 
     CGRect aFrame = profileView.frame; 
     aFrame.origin.x = 0; 
     profileView.frame = aFrame; 

     [UIView commitAnimations]; 


    } 



} 

enter image description here
這裏按下按鈕和視圖拖動努力增加x位置 enter image description here

他重新圖像結束拖動過程 現在如果再次按下白色子視圖左側的按鈕,它將再次出現x = 0的位置。 我希望它能幫助你!

+0

謝謝你爲我工作 – 2013-03-20 08:06:18