RK-
RK-

Reputation: 12231

When to change the frame size of a UIView while custom drawing it?

I am doing a custom draw in my App for a View in drawRect method.

I capture user's pinch gesture and based on that, I wanted to alter the the frame size of the View each time I do that.

I tried changing the UIView's frame in drawRect method. But I felt that is wrong place to do, because drawRect asks us to draw in a particular rect.

I did create a protocol and when ever I captured, I tried changing the Frame Size of the View from the view controller with the help of the protocol methods. But that also did not work.

How can I achieve this?

UPDATE:

I was able to change the UIView's frame from awakeFromNib method of Custom UIView. But I have necessity to change the frame regulary based on users' actions, and did not find any ways to reload awakeFromNib method..

Upvotes: 1

Views: 1460

Answers (1)

chown
chown

Reputation: 52778

These are just examples and would need to be modified to fit your specific situation, but you could probably use either CGRectApplyAffineTransform or UIView animations:

- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
    UIView *senderView = sender.view;
    if (sender.state == UIGestureRecognizerStateBegan) {
        initialFrame = senderView.frame;
    }
    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
    senderView.frame = CGRectApplyAffineTransform(initialFrame, zt);
    return;
}

/* -------------------------------------------------------------------------- */

- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
    UIView *senderView = sender.view;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.35f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

    /* Set changes for the animation before calling commitAnimations
    self.tableView.layer.borderWidth = 3.0;
    self.tableView.layer.borderColor = [[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.85] CGColor];
    self.tableView.layer.backgroundColor = [[UIColor colorWithWhite:0.2 alpha:0.5] CGColor];
    self.tableView.layer.shadowOffset = CGSizeMake(4.0, 2.0);
    self.tableView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.tableView.layer.shadowOpacity = 1.0;
    self.tableView.layer.shadowRadius = 2.0;
    self.tableView.layer.cornerRadius = 10.0;
    */

    [UIView commitAnimations];
}

Upvotes: 1

Related Questions