1

嗨,我是 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];
}
4

1 回答 1

0

这是我在 ios 中的 superview 上拖动视图的代码。假设我在子视图上有一个按钮。当这个按钮点击这个动作时。它检查子视图是否放置在 x==0 它动画它到 x=260,反之亦然注意你必须添加框架 QuartzCore/QuartzCore 和#import你的 action.setAnimationDuration 的 .m 文件中的 QuartzCore/QuartzCore.h 显示拖动的持续时间 -(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];


    }



}

在此处输入图像描述
这里按钮按下和视图正在向增加 x 位置拖动 在此处输入图像描述

这里图像结束拖动过程现在,如果您再次按下白色子视图左侧上方的按钮,它将再次出现在 x=0 位置。我希望它会帮助你!

于 2013-03-20T07:47:49.763 回答