何时在自定义绘图时更改UIView的帧大小?

时间:2011-10-15 15:48:17

标签: ios uiview drawrect

我正在我的应用程序中为drawRect方法中的View进行自定义绘制。

我捕获用户的捏合手势,基于此,我想每次都改变视图的帧大小。

我尝试在drawRect方法中更改UIView的框架。但我觉得这是错误的地方,因为drawRect要求我们画一个特定的矩形。

我确实创建了一个协议,当我捕获时,我尝试在协议方法的帮助下从视图控制器更改视图的帧大小。但这也行不通。

我怎样才能做到这一点?

更新

我能够从Custom UIView的awakeFromNib方法更改UIView的框架。但我有必要根据用户的操作定期更改框架,并且没有找到任何重新加载awakeFromNib方法的方法。

1 个答案:

答案 0 :(得分:1)

这些只是示例,需要根据您的具体情况进行修改,但您可以使用CGRectApplyAffineTransformUIView动画:

- (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];
}
相关问题