代码之家  ›  专栏  ›  技术社区  ›  rano

UIView beginanimations:子视图不应设置动画

  •  0
  • rano  · 技术社区  · 14 年前

    initWithFrame: 方法如下:

    UIButton * dropB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    dropB.frame = CGRectMake(190, 22, 52, 22);
    dropB.backgroundColor = [UIColor clearColor];
    dropB.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:11];
    [dropB setTitle:@"DROP" forState:UIControlStateNormal];
    [dropB addTarget:viewController 
              action:@selector(dropSelectedObject)
    forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:dropB];
    

    [UIView beginAnimations:@"MoveIt" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.dropButton.center = CGPointMake(self.dropButton.center.x + 80, self.dropButton.center.y -10);
    self.frame = CGRectMake(0, 0, WIDTH, HEIGHT); 
    self.center = CGPointMake(CENTER_X, CENTER_Y);
    [UIView commitAnimations];
    

    按钮只会被转换到一个新的位置,即上一个位置(我猜原始坐标是相对于视图的)。我怎样才能使它转换到我指定的点?

    1 回复  |  直到 14 年前
        1
  •  0
  •   rano    14 年前

    好吧,这很简单(也很愚蠢)。

    因为我需要UIButton保持一个固定的高度,所以在调整大小时我必须指定什么空间是“灵活的”。可以使用 autoresizingMask UIView的以下属性:

    dropB.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin;