views:

618

answers:

1

So, I have a UIView subclass called MovableBlock. It overrides touchesBegan, touchesMoved, touchesEnded and touchesCancelled. The moving about part works just fine.

When a MovableBlock is moved to the left side of the screen, it's 130px wide. When it's moved to the right side of the screen, it's 80px wide. I'd like to animate the transition in size, so that as the user is dragging the view around, the MovableBlock smoothy adjusts its size.

Right now, I have this:

// Handles the start of a touch
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
}

// Handles the continuation of a touch.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
    CGPoint svpt = [[touches anyObject] locationInView:self.superview];
    CGPoint pt = [[touches anyObject] locationInView:self];
    if (svpt.x < 130) {
        [self setColumn:0];
    }
    else {
        [self setColumn:1];
    }

    CGRect frame = [self frame];
    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;

    [self setFrame: frame];
}

-(void)setColumn: (NSInteger)newColumn {
    column = newColumn;
    if (column != newColumn) {
        [UIView beginAnimations:@"Resize Block" context:nil];
        [UIView setAnimationDuration:0.2];

        if (column == 0) {
            [self setBounds: CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 130, self.bounds.size.height)];
        }
        else {
            [self setBounds: CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 80, self.bounds.size.height)];
        }
        [UIView commitAnimations];
    }
}

startPosition and column are both members of the class.

While dragging the MovableBlock around, it doesn't resize (or animate). I can't seem to find any reason why it shouldn't work, though I also can't find examples of doing this, so maybe there's some subtle trick that I'm missing.

There is another animation in the touchesEnded method that works just fine, so I know that the animation part of things is working, just not in touchesMoved.

Thanks for any help!

A: 

I seem to have figured it out. Using setBounds in the setColumn method didn't seem to get noticed by the susequent call of frame = [self frame]. Moving the frame declaration to the top, above the calls to setColumn and restructuring how to determine the width of the current column solved things.

Also, don't mix frames and bounds. You'll just confuse your headmeats. :-)

Tim Sullivan