如何将对象从一个视图拖动到另一个视图

时间:2013-05-28 18:55:20

标签: iphone ios objective-c ipad cocoa-touch

您好我是ios开发的新手。我正在尝试将标签从视图拖动到工具栏...但是标签将转到工具栏的背面。我希望标签拖动工具栏的前面..请任何有关此问题的帮助我.....这是我到目前为止所做的代码

- (void)viewDidLoad {
    [super viewDidLoad];

    UIToolbar *backgrdtoolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,44)];

    [backgrdtoolbar setBarStyle:UIBarStyleBlackOpaque];

    [self.view addSubview:backgrdtoolbar];

    mainScroll =  [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];

    [mainScroll setContentSize:CGSizeMake(600,45)];
    [mainScroll setBackgroundColor:[UIColor clearColor]];
    [mainScroll setShowsHorizontalScrollIndicator:NO];

    mainToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 600, 44)];
    [mainToolbar setBarStyle:UIBarStyleBlackTranslucent];

    [self.view addSubview:mainScroll];

    [mainScroll addSubview:mainToolbar];

    UIBarButtonItem *b1 = [[UIBarButtonItem alloc]initWithTitle:@"1" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)];
    UIBarButtonItem *b2 = [[UIBarButtonItem alloc]initWithTitle:@"2" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)];
    UIBarButtonItem *b3 = [[UIBarButtonItem alloc]initWithTitle:@"3" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)];

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:b1,spacer,b2,spacer,b3, spacer,b4,spacer,b5,spacer,b6,spacer,b7,spacer,b8,spacer,b9,spacer,b10,nil];
    [mainToolbar setItems:arr];

}



 - (IBAction)handle:(UIPanGestureRecognizer *)gesture {
        static CGRect originalFrame;

        if (gesture.state == UIGestureRecognizerStateBegan){
            originalFrame = gesture.view.frame;
        } else if (gesture.state == UIGestureRecognizerStateChanged) {
            CGPoint translate = [gesture translationInView:gesture.view.superview];

            CGRect newFrame = CGRectMake(fmin(gesture.view.superview.frame.size.width - originalFrame.size.width, fmax(originalFrame.origin.x + translate.x, 0.0)),
                                         fmin(gesture.view.superview.frame.size.height - originalFrame.size.height, fmax(originalFrame.origin.y + translate.y , 0.0)),
                                         originalFrame.size.width,
                                         originalFrame.size.height);


            gesture.view.frame = newFrame;

        }

    }

1 个答案:

答案 0 :(得分:0)

可以将一个视图拖到另一个视图吗?是的,这是可能的。

但是,如果唯一重要的是将标签放在另一个视图的前面,我相信最好的方法不是改变你的标签所在的视图。我的意思是你不需要更改目标视图中的其他视图。

如果我在你的鞋子里,我将使用一个视图,其中包含放置标签以包含两个视图的视图,放置标签的视图以及您希望拖动标签的视图。< / p>

当您将Label添加到此视图时,需要注意的重点是它必须位于您的视图前面,您可以使用此代码执行此操作:

[view bringSubviewToFront:yourLabel];

之后,您可以将其拖动到视图容器中,它将位于其他视图的前面。 :)

-

如果您确实要将其从一个视图更改为另一个视图,则必须将其从原始视图中删除,然后将其添加到将要放置的另一个视图中。像这样的东西

if(gesture.view.frame.origin.y > y ) {
    [gesture.view removeFromSuperview];
    [newView addSubview:gesture.view];
    //Your code to adjust the frame for the new view, because your frame is relative
    //It is define by your superview
}