我在我的项目中使用UIImageview实现缩放和旋转, 我在旋转图像时遇到放大和缩小问题,
以下是我的代码:
<。>文件中的
@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;
<。>文件中的
- (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);
}
请查看随附的图片供您参考。
答案 0 :(得分: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);
}
答案 1 :(得分:1)
我建议使用与您拥有的旋转代码非常相似的方法缩放图像:
CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);
只需向上发送1.0以上即可向上扩展,小于1.0可向下扩展;
答案 2 :(得分: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);
}