Ios 在自定义绘制UIView时,何时更改其框架大小?

Ios 在自定义绘制UIView时,何时更改其框架大小?,ios,uiview,drawrect,Ios,Uiview,Drawrect,我正在我的应用程序中为drawRect方法中的视图进行自定义绘制 我捕捉到了用户的收缩手势,并基于此,我想在每次这样做时改变视图的帧大小 我尝试在drawRect方法中更改UIView的框架。但我觉得这样做是不对的,因为drawRect要求我们在特定的rect中绘制 我确实创建了一个协议,当我捕获时,我尝试在协议方法的帮助下从视图控制器更改视图的帧大小。但这也不起作用 我怎样才能做到这一点 更新: 我能够从自定义UIView的awakeFromNib方法更改UIView的框架。但是我必须根据用户

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

我捕捉到了用户的收缩手势,并基于此,我想在每次这样做时改变视图的帧大小

我尝试在drawRect方法中更改UIView的框架。但我觉得这样做是不对的,因为drawRect要求我们在特定的rect中绘制

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

我怎样才能做到这一点

更新:


我能够从自定义UIView的awakeFromNib方法更改UIView的框架。但是我必须根据用户的操作定期更改帧,并且没有找到任何方法从NIB方法重新加载唤醒。

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

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

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

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

谢谢你的回答+我同意。但我还有另一个疑问,我不想根据用户的挤压增加框架的大小。相反,我想在某些情况下将帧大小更改为某个CGRect。如何将CGrect应用于此转换?谢谢您的回答+我同意。但我还有另一个疑问,我不想根据用户的挤压增加框架的大小。相反,我想在某些情况下将帧大小更改为某个CGRect。如何将CGrect应用于此转换?