简体   繁体   English

如何在touches上将控件从一个uiview切换到另一个uiview

[英]How to shift control from one uiview to another in touchesMoved method on iPhone

Hi I am new in iPhone dev. 嗨,我是iPhone开发人员中的新手。 Please could any body tell me if I have two views one on another and I want that when I click super view it will drag and when I click subview it will drag. 请任何人告诉我,如果我有两个视图,另一个视图,我希望当我单击超级视图时它将拖动,而当我单击子视图时将拖动。 Can anybody help me? 有谁能够帮助我?

Here is my code: 这是我的代码:

-(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];
}

here is my code to drag view on superview in ios. 这是我的代码,用于在ios中的超级视图上拖动视图。 Suppose i have a button on subview.when this button tapped this action called.It check if subview is placed at x==0 it animated it to x=260,and vice versa Note also you must add framework QuartzCore/QuartzCore and #import QuartzCore/QuartzCore.h in .m file of your action.setAnimationDuration show duration of dragging -(IBAction)listButtonTapped{ 假设我在subview上有一个按钮,当点击此按钮时调用此操作。它检查子视图是否位于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];


    }



}

在此处输入图片说明
here button press and view is dragging towards increasing x position 在这里按下按钮,视图朝着增加的x位置拖动 在此处输入图片说明

here image end dragging process Now if you again press button above white subview left side it will again come on x=0 position. 此处图像结束拖动过程现在,如果再次按下白色子视图左侧上方的按钮,它将再次位于x = 0位置。 I hope it will help you! 希望对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM