天天看点

IOS 形变

// CGAffineTransformTranslate 位置移动,改变frame中original的坐标点,但是center值没变,因此如果要发生形变的话,不能在此基础上改变,这时必须通过改变center的值来实现位置移动
// CGAffineTransformScale 按比例改变frame中size的大小
// CGAffineTransformRotate 旋转
// C和OC语言里,case中不能定义变量
- (IBAction)actionEvent:(UIButton *)sender {
    CGAffineTransform transform;
    CGPoint center;
    switch (sender.tag) {
        case 0:
            center = _imageView.center;
            center.y -= 10;
//            NSLog(@"%@",NSStringFromCGPoint(_imageView.center));
//            NSLog(@"%@",NSStringFromCGPoint(_imageView.frame.origin));
//            transform = CGAffineTransformTranslate(_imageView.transform, 10, 10);
//            _imageView.transform = transform;
//            NSLog(@"%@",NSStringFromCGPoint(_imageView.frame.origin));
//            NSLog(@"%@",NSStringFromCGPoint(_imageView.center));
            break;
        case 1:
            center = _imageView.center;
            center.y += 10;
            break;
        case 2:
            center = _imageView.center;
            center.x -= 10;
            break;
        case 3:
            center = _imageView.center;
            center.x += 10;
            break;
        case 4:
            transform = CGAffineTransformScale(_imageView.transform, 1.2, 1.2);
            break;
        case 5:
            transform = CGAffineTransformScale(_imageView.transform, 1/1.2, 1/1.2);
            break;
        case 6:
            transform = CGAffineTransformRotate(_imageView.transform, M_PI_4);
            break;
        case 7:
            transform = CGAffineTransformRotate(_imageView.transform, -M_PI_4);
            break;
            
        default:
            break;
    }
    if (sender.tag < 4) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        _imageView.center = center;
        [UIView commitAnimations];
    } else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        _imageView.transform = transform;
        [UIView commitAnimations];
    }
}
           

IOS中形变默认的锚点是中心点,对应的属性是center,但是通过 CGAffineTransformTranslate 属性来实现 位置移动,是改变 frame 中 original 的坐标点 , 但是 center 值没变(其实应该是需要改变的,但是不知道苹果为啥没改变,这个有待考究),因此如果要发生形变(放大,缩小,旋转)的话,不能在此基础上改变,这时必须通过改变 center 的值来实现位置移动