Question

I have UIView which is transform vertically by using

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

Now I need to move this UIView from one location to other by using touches, for this I have used

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

}

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

}

But I am not able to maintain the transform, How we can achive this?

Basically I have added some UI elements on UIView using following call

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

}

Now I am looking to Move these added UIViews having transform.

Was it helpful?

Solution

I'm not sure what you mean by "I am not able to maintain the transform" Anyway, here is a slightly different approach that I think may help you.

For starters, when you apply to a view a transform other than the 'Identity' transform the frame property becomes meaningless. This means you cannot use its origin member to change the view's position. You have to use the view's center property instead.

Also, for dragging, I strongly recommend you use a UIPanGestureRecognizer instead of the touches... methods. This is because the gesture recognizer maintains state for you and it's super easy to drag things around.

Here is some example code:

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

I tested this code in a project of mine with a view that has a rotation transform and it works perfectly.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top