0

我在我的项目中使用 UIImageview 实现缩放和旋转,我在旋转图像后面临放大和缩小问题,

这是我的代码如下:

在 .h 文件中

@interface ViewController : UIViewController{
 float degrees;
 float height;
 float width;

float moveLeft;
float moveRight;
}

@property(nonatomic,retain)UIImageView *imageView;

-(IBAction)rotationLeft:(id)sender;
-(IBAction)rotationRight:(id)sender;
-(IBAction)zoomIn:(id)sender;
-(IBAction)zoomOut:(id)sender;

-(IBAction)moveLeft:(id)sender;
-(IBAction)moveRight:(id)sender; 

在 .m 文件中

- (void)viewDidLoad
{
[super viewDidLoad];



height=50;
width=50;
degrees=20;
moveLeft=20;
moveRight=20;
imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
imageView.frame=CGRectMake(100, 100,width, height);
[self.view addSubview:imageView];



// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)rotationLeft:(id)sender{
//the value in degrees
imageView.transform = CGAffineTransformMakeRotation(degrees*M_PI/180);
degrees=degrees+25;
}

-(IBAction)rotationRight:(id)sender{
//the value in degrees
degrees=degrees-25;
imageView.transform = CGAffineTransformMakeRotation(degrees*M_PI/180);

}
-(IBAction)zoomIn:(id)sender{
height=height-15;
width=width-15;
imageView.frame=CGRectMake(100, 100,width, height);
}
-(IBAction)zoomOut:(id)sender{
height=height+15;
width=width+15;
imageView.frame=CGRectMake(100, 100,width, height);
}

请找到随附的图片供您参考。在此处输入图像描述

4

3 回答 3

1

您应该CGAffineTransformMakeScale用于缩放目的,而不是强制框架。

然后在某个地方定义一个全局foal x = 1;

-(IBAction)zoomIn:(id)sender{
   x += 0.3;
   imageView.transform = CGAffineTransformMakeScale(x, x);
}

-(IBAction)zoomOut:(id)sender{
   x -= 0.3;
   imageView.transform = CGAffineTransformMakeScale(x, x);
}
于 2012-07-19T11:05:04.777 回答
1

我建议使用与您拥有的旋转代码非常相似的方法来缩放图像:

CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);

发送大于 1.0 放大,小于 1.0 缩小;

于 2012-07-19T11:06:18.997 回答
1

下面的代码对我来说很完美!!!

-(IBAction)rotationLeft:(id)sender{
//the value in degrees
    degrees=degrees+25;
    CGAffineTransform t;
    t=CGAffineTransformMakeScale(x, x);
    // imageView.transform = CGAffineTransformMakeRotation(degrees*M_PI/180,x,x);
    imageView.transform=CGAffineTransformRotate(t, degrees*M_PI/180);
}

-(IBAction)rotationRight:(id)sender{
    degrees=degrees-25;
    CGAffineTransform t;
    t=CGAffineTransformMakeScale(x, x);
    imageView.transform=CGAffineTransformRotate(t, degrees*M_PI/180);

 }
 -(IBAction)zoomIn:(id)sender{
    x += 0.3;
    CGAffineTransform t;
    t=CGAffineTransformMakeRotation(degrees*M_PI/180);
    imageView.transform=CGAffineTransformScale(t, x, x);
 }

 -(IBAction)zoomOut:(id)sender{
     x -= 0.3;
    CGAffineTransform t;
    t=CGAffineTransformMakeRotation(degrees*M_PI/180);
    imageView.transform=CGAffineTransformScale(t, x, x);
 }
于 2012-07-20T06:17:33.710 回答