2

我有 UIView 它是通过使用垂直变换

currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0f)));

现在我需要通过触摸将这个 UIView 从一个位置移动到另一个位置,为此我使用过

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

}

但是我无法保持转换,我们如何实现这一点?

基本上,我使用以下调用在 UIView 上添加了一些 UI 元素

-(void)AddItemOnView:(UIView*)aView
               Angle:(CGFloat)aDegree
             XOrigin:(CGFloat)aXOrigin
             YOrigin:(CGFloat)aYOrigin
               Width:(CGFloat)aWidth
              Height:(CGFloat)aHeight
               FlipX:(CGFloat)aFlipHorrizontal
               FlipY:(CGFloat)aFlipVerticle
{
    UIView* currentView = aView;
    if(currentView)
    {
        CGFloat angle = aDegree;
        CGFloat flipHorrizontal = aFlipHorrizontal;
        CGFloat flipVerticle = aFlipVerticle;
        CGFloat xOrigin = aXOrigin;
        CGFloat yOrigin = aYOrigin;
        CGFloat width = aWidth;
        CGFloat height = aHeight;

        currentView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
        currentView.frame = CGRectIntegral(CGRectMake(0, 0, width, height));

        /* Flip The View Horrizontly*/
        if(flipHorrizontal < 0)
        {
            /* Concat With previous Layer Operation */

            currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0f)));

            /* Need to set anchor point ==> Top Right Corner */
            currentView.layer.anchorPoint = CGPointMake(1.0f, 0.0f);

        }

        /* Flip The View Verticaly*/
        if(flipVerticle < 0)
        {
            /* Concat With previous Layer Operation */

            currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0f)));

            if(flipHorrizontal < 0)
            {
                /* This needs to set as we have already done flip X */

                /* Need to set anchor point ==> Bottom Right Corner */
                currentView.layer.anchorPoint = CGPointMake(1.0f, 1.0f);
            }
            else
            {
                /* Need to set anchor point ==> Bottom Left Corner */
                currentView.layer.anchorPoint = CGPointMake(0.0f, 1.0f);
            }

        }

        /* Perform Rotation */
        if(angle != 0)
        {
            /* Concat With previous Layer Operation */

            currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(DegreesToRadians(angle), 0, 0, 1.0)));

            if(flipHorrizontal < 0 || flipVerticle < 0)
            {
                /* Countinue with previous anchor point */
            }
            else
            {
                /* Need to set anchor point ==> Top Left Corner */
                currentView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
            }

        }

        /* Set Origins of View */
        currentView.layer.position = CGPointMake(xOrigin, yOrigin);
        [self addSubview:currentView];
    }

}

现在我正在寻找移动这些添加的具有变换的 UIView。

4

1 回答 1

4

我不确定您所说的“我无法保持转换”是什么意思无论如何,这里有一种稍微不同的方法,我认为它可能会对您有所帮助。

对于初学者,当您将“身份”转换以外的转换应用于视图时,该frame属性变得毫无意义。这意味着您不能使用其origin成员来更改视图的位置。您必须改用视图的center属性。

另外,对于拖动,我强烈建议您使用 aUIPanGestureRecognizer而不是touches...方法。这是因为手势识别器为您维护状态,并且非常容易拖动东西。

这是一些示例代码:

// Create your view and apply all the transforms you want
// --code here--

// Create and assign the UIPanGestureRecognizer
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
[self.currentView addGestureRecognizer:panGesture];

// Here is where the dragging happens
-(void)panDetected:(UIPanGestureRecognizer*)panGesture {

    // Get the view that detected the gesture
    UIView *view = panGesture.view;

    // If dragging started or changed...
    if (panGesture.state == UIGestureRecognizerStateBegan || panGesture.state == UIGestureRecognizerStateChanged) {

        // Get the translation in superview coordinates
        CGPoint translation = [panGesture translationInView:view.superview];

        // Get your view's center
        CGPoint viewCenter = view.center;

        // Add the delta 
        viewCenter.x += translation.x;
        viewCenter.y += translation.y;
        view.center = viewCenter;

        // Reset delta from the gesture recognizer
        [panGesture setTranslation:CGPointZero inView:view.superview];
    }
}

我在我的一个项目中测试了这段代码,它的视图具有旋转变换并且效果很好。

希望这可以帮助!

于 2013-10-28T19:27:37.243 回答