在iOS7中,是否无法将ImageView从父视图的约束拖动到主体?

在iOS7中,是否无法将ImageView从父视图的约束拖动到主体?,ios,objective-c,drag-and-drop,uiimageview,uigesturerecognizer,Ios,Objective C,Drag And Drop,Uiimageview,Uigesturerecognizer,在iOS中,当使用滚动视图中的uipangestureerecognizer拖动UIImageView(另一个UIImageView中底部的条形图)中的UIImageView(红色方块)时,我无法从条形图的约束中拖动到位于上方的主体UIView?任何帮助都将不胜感激。谢谢 为清楚起见,以下是我的视图结构: view (main controller) | ——> view (nav bar) | ——> view (main body) | ——>

在iOS中,当使用
滚动视图中的
uipangestureerecognizer
拖动
UIImageView
(另一个
UIImageView
中底部的条形图)中的
UIImageView
(红色方块)时,我无法从条形图的约束中拖动到位于上方的主体
UIView
?任何帮助都将不胜感激。谢谢

为清楚起见,以下是我的视图结构:

view (main controller)
  |  
  ——> view (nav bar)
  |
  ——> view (main body)
  |
  ——> view (yellow bar)
              |——> scroll view (dynamic)
                     |
                     ——>view (dynamic - wider than scroll view)
                          |
                          ——> image view (dynamically created red square)
这看起来像:

我在做什么:

基本上,在
ViewController
.m中,我以编程方式创建一个
scrollview
barScrollView
)并将其添加到视图中(黄色条)。我动态创建另一个新视图并将其传递给一个函数,在该函数中,在一个循环中,我动态创建几个
ImageView
项(红色方块),在它们上启用
userInteractionEnabled
,创建
UIPangestureRecognitor
,并将其添加到创建的每个
ImageView
,然后将它们添加到传入的视图中。最后,我添加了传递到
scrollview

我的代码:

在故事板中:

- dragged in 3 UIViews (see image above)
在ViewController.h中:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *navBar;
@property (weak, nonatomic) IBOutlet UIView *mainBodyView;
@property (weak, nonatomic) IBOutlet UIView *yellowBar;

@end
#导入
@界面ViewController:UIViewController
@属性(弱、非原子)IBUIView*导航条;
@属性(弱、非原子)IBUIView*mainBodyView;
@属性(弱、非原子)IBUIVIEW*黄条;
@结束
在ViewController.m中:

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect barRect = CGRectMake(0, 0, 320, 60);
    CGRect containerRect = CGRectMake(0, 0, 820, 60);
    UIScrollView *barSV = [[UIScrollView alloc] initWithFrame:barRect];     
    [_yellowBar addSubview:barSV];
    UIView *redSquaresContainerView = [[UIScrollView alloc] initWithFrame:containerRect];
    [self generateRedSquares:redSquaresContainerView]; 
    [barSV addSubview:redSquaresContainerView];
    barSV.contentSize =containerRect.size;
}


-(void)generateRedSquares:(UIView*) containerView{
    int x =0;
    for( int i = 0; i < 5; i++ ) {
        CGRect redSquareRect = CGRectMake(x, 10, 30, 30);
        UIImageView* aRedSquareView = [[UIImageView alloc] initWithFrame:redSquareRect];
        aRedSquareView.backgroundColor = [UIColor redColor];    
        aRedSquareView.userInteractionEnabled = YES; 
        UIPanGestureRecognizer *dragRecognizer = [[UIPanGestureRecognizer      
          alloc]initWithTarget:self action:@selector(dragRedSquare:)];
        [aRedSquareView addGestureRecognizer:dragRecognizer];
        [containerView addSubview:aRedSquareView];
        x+=60;
    }
}


- (void) dragRedSquare:(UIPanGestureRecognizer*)recognizer{
    static CGRect originalFrame;
    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        originalFrame = recognizer.view.frame;
    } else if ([recognizer state] == UIGestureRecognizerStateChanged) {
        // Drag moved
        CGPoint translate = [recognizer translationInView:recognizer.view];
        CGRect newFrame = CGRectMake(originalFrame.origin.x + translate.x,
                                 originalFrame.origin.y + translate.y,
                                 originalFrame.size.width,
                                 originalFrame.size.height);    
        if (CGRectContainsRect(recognizer.view.superview.bounds, newFrame)){
            recognizer.view.frame = newFrame;
        }
    } else if ([recognizer state] == UIGestureRecognizerStateEnded) {
        // Drag completed
    }    
}
-(void)viewDidLoad{
[超级视图下载];
CGRect-barRect=CGRectMake(0,0320,60);
CGRect containerRect=CGRectMake(0,0820,60);
UIScrollView*barSV=[[UIScrollView alloc]initWithFrame:barRect];
[_yellowbaraddsubview:barSV];
UIView*redSquaresContainerView=[[UIScrollView alloc]initWithFrame:containerRect];
[自生方形:redSquaresContainerView];
[barSV addSubview:redSquaresContainerView];
barSV.contentSize=containerRect.size;
}
-(void)generateRedSquares:(UIView*)containerView{
int x=0;
对于(int i=0;i<5;i++){
CGRect redSquareRect=CGRectMake(x,10,30,30);
UIImageView*aRedSquareView=[[UIImageView alloc]initWithFrame:redSquareRect];
aRedSquareView.backgroundColor=[UIColor redColor];
aRedSquareView.userInteractionEnabled=是;
UIPanGestureRecognizer*dragrerecognizer=[[UIPanGestureRecognizer]
alloc]initWithTarget:self action:@selector(dragRedSquare:)];
[aRedSquareView addgestureRecognitor:DragRecogniter];
[containerView添加子视图:aRedSquareView];
x+=60;
}
}
-(无效)dragRedSquare:(UIPangestureRecognitor*)识别器{
静态CGRect原始帧;
if([Recognitor state]==UIgestureRecognitzerState开始){
originalFrame=recognizer.view.frame;
}else if([Recognitor state]==UIgestureRecognitzerStateChanged){
//拖动移动
CGPoint translate=[recognizer translationView:recognizer.view];
CGRect newFrame=CGRectMake(originalFrame.origin.x+translate.x,
originalFrame.origin.y+translate.y,
原始帧。大小。宽度,
原始框架、尺寸、高度);
if(CGRectContainsRect(recognizer.view.superview.bounds,newFrame)){
recognizer.view.frame=newFrame;
}
}else if([Recognitor state]==UIgestureRecognitzerStateEnded){
//拖动完成
}    
}

如果您使用的是故事板,请确保UIElement位于您希望它浏览的视图下方

例如,在下图中,我希望将UIImage绘制在所有其他视图上,以便将其拖动到屏幕上的所有其他视图上


如果您想将ImageView从的约束中拖出,然后尝试将其置于主视图的字体中,而不是作为孩子添加它,您可能需要作为兄弟姐妹在视图中进行平移

这对我很有效,希望能帮助其他人。不管怎样,如果你正在使用滚动视图/手势,并且被卡住了,观看下面的视频确实很有帮助。(我稍后会详细说明……谢谢)

在dragRedSquare函数中,在if语句中包含以下两行:

- (void) dragRedSquare:(UIPanGestureRecognizer*)recognizer{

...

if ([recognizer state] == UIGestureRecognizerStateBegan) {
    recognizer.view.center = [self.view convertPoint:recognizer.view.center fromView:recognizer.view.superview];
    [self.view addSubview:recognizer.view];
}

...

}
资源:
(高级滚动视图和触摸处理技术:~28分钟标记)

Hi。谢谢你的建议。我不知道我是否明白你的意思,但我有我的视图(红色方块),我将把我的滚动视图和红色方块放在故事板的底部。你选择了剪辑子视图吗?谢谢,吉特。你是不是建议我把ImageView(红色sq)放在前面?它需要位于我要添加到scrollview的视图中,这样我可以动态填充它(如果有太多的话,可以滚动)。是的,尝试将它放在前面,不要将它添加到任何容器视图中,将它添加为要将其拖动到Hi Geet的视图的同级。谢谢我需要将(红色sq)图像视图添加到容器视图中,以便滚动工作。我试着包括[self.view-bringsubview-tofront:aRedSquareView];在我的for循环中,GeneratoredSquares无效。