1. 程式人生 > >IOS 控制元件帶動畫移動

IOS 控制元件帶動畫移動

-(IBAction)move:(UIButton *) button {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    // 不允許直接修改某個物件的結構體成員
    CGRect tempFrame = self.image.frame;
    if (button.tag == 1) {
        // 上
        NSLog(@"上");
        tempFrame.origin.y = tempFrame.origin.y - 10;
    } else if (button.tag == 2) {
        // 右
        NSLog(@"右");
        tempFrame.origin.x = tempFrame.origin.x + 10;
    } else if (button.tag == 3) {
        // 下
        NSLog(@"下");
        tempFrame.origin.y = tempFrame.origin.y + 10;
    } else if (button.tag == 4) {
        // 左
        NSLog(@"左");
        tempFrame.origin.x = tempFrame.origin.x - 10;
    }
    self.image.frame = tempFrame;
    
    [UIView commitAnimations];
}