Ios 使用UIPanGesture更新视图的NSLayoutConstant

Ios 使用UIPanGesture更新视图的NSLayoutConstant,ios,objective-c,autolayout,nslayoutconstraint,Ios,Objective C,Autolayout,Nslayoutconstraint,我有一个UIView,我添加了许多UIImageViews(示例5,数字是动态的)作为子视图。 所有UIImageView都添加了NSLayoutConstraint(大小调整和中心与superView中心相关)。所有与中心相关的约束都添加到ui视图(父视图) 每个UIImageView都有一个panGesture。因此,当我移动UIImageView时,必须更新该UIImageView的中心相关约束 问题是, 如何识别移动UIImageView的约束?? (我是否需要保留所有已创建约束的引用?

我有一个
UIView
,我添加了许多
UIImageView
s(示例5,数字是动态的)作为子视图。 所有UIImageView都添加了
NSLayoutConstraint
(大小调整和中心与superView中心相关)。所有与中心相关的约束都添加到
ui视图
(父视图)

每个
UIImageView
都有一个panGesture。因此,当我移动
UIImageView
时,必须更新该
UIImageView
的中心相关约束

问题是,

如何识别移动
UIImageView的约束?

(我是否需要保留所有已创建约束的引用?或者是否有其他方法进行此操作?)

编辑:我的解决方案

感谢@Lord Zsolt的回答。 我写了下面的代码(我的问题的解决方案)。它可以帮助其他有同样问题的人

定义为全局变量

NSLayoutConstraint *selectedImageViewCenterXconstraint, *selectedImageViewCenterYconstraint;
那么UIPangestureRecognitor选择器方法是:

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

    UIImageView *senderView = (UIImageView*)recognizer.view;

    if (recognizer.state == UIGestureRecognizerStateBegan) {
        selectedImageViewCenterXconstraint = nil;
        selectedImageViewCenterYconstraint = nil;
        NSArray *viewConstraints = self.view.constraints;
        for (NSLayoutConstraint *constraint in viewConstraints) {
            if ([constraint.firstItem isEqual:senderView]) {
                if (constraint.firstAttribute == NSLayoutAttributeCenterX) {
                    NSLog(@"gotCenterX");
                    selectedImageViewCenterXconstraint = constraint;
                }else if (constraint.firstAttribute == NSLayoutAttributeCenterY) {
                    NSLog(@"gotCenterY");
                    selectedImageViewCenterYconstraint = constraint;
                }
            }

            if (selectedImageViewCenterXconstraint && selectedImageViewCenterYconstraint) {
                break;
            }
        }
    }


    CGPoint translation = [recognizer translationInView:senderView.superview];

    CGFloat newXconst,newYconst;
    newXconst = selectedImageViewCenterXconstraint.constant + translation.x;
    newYconst = selectedImageViewCenterYconstraint.constant + translation.y;

    selectedImageViewCenterXconstraint.constant = newXconst;
    selectedImageViewCenterYconstraint.constant = newYconst;
    [self.view layoutIfNeeded];

    [recognizer setTranslation:CGPointMake(0, 0) inView:senderView.superview];
} 

UIView子类有一个属性
constraints
,它为您提供一个
NSArray
,其中包含添加到该视图上的约束

您可以在该数组中搜索约束,并验证它是您的
UIImageView

代码示例:

//Assuming self is a view controller.
NSArray *constraints = [self.view constraints];
for (NSLayoutConstraint *constraint in constraints) {
    if (constraint.firstItem == myImageView ||
        constraint.secondItem == myImageView) {
        NSLog(@"Constraint belongs to myImageView");
    }
}

UIView子类有一个属性
constraints
,它为您提供一个
NSArray
,其中包含添加到该视图上的约束

您可以在该数组中搜索约束,并验证它是您的
UIImageView

代码示例:

//Assuming self is a view controller.
NSArray *constraints = [self.view constraints];
for (NSLayoutConstraint *constraint in constraints) {
    if (constraint.firstItem == myImageView ||
        constraint.secondItem == myImageView) {
        NSLog(@"Constraint belongs to myImageView");
    }
}

UIView子类有一个属性
constraints
,它为您提供一个
NSArray
,其中包含添加到该视图上的约束

您可以在该数组中搜索约束,并验证它是您的
UIImageView

代码示例:

//Assuming self is a view controller.
NSArray *constraints = [self.view constraints];
for (NSLayoutConstraint *constraint in constraints) {
    if (constraint.firstItem == myImageView ||
        constraint.secondItem == myImageView) {
        NSLog(@"Constraint belongs to myImageView");
    }
}

UIView子类有一个属性
constraints
,它为您提供一个
NSArray
,其中包含添加到该视图上的约束

您可以在该数组中搜索约束,并验证它是您的
UIImageView

代码示例:

//Assuming self is a view controller.
NSArray *constraints = [self.view constraints];
for (NSLayoutConstraint *constraint in constraints) {
    if (constraint.firstItem == myImageView ||
        constraint.secondItem == myImageView) {
        NSLog(@"Constraint belongs to myImageView");
    }
}

为要设置动画/引用的约束创建属性是完全可以接受的,而不仅仅是解决方法。假设你得到了想要的结果,这段代码看起来非常好。为你想要设置动画/引用的约束创建属性是完全可以接受的,而不仅仅是一种解决方法。假设你得到了想要的结果,这段代码看起来非常好。为你想要设置动画/引用的约束创建属性是完全可以接受的,而不仅仅是一种解决方法。假设你得到了想要的结果,这段代码看起来非常好。为你想要设置动画/引用的约束创建属性是完全可以接受的,而不仅仅是一种解决方法。假设您得到想要的结果,这段代码看起来非常好。