0

我需要UIImageViewUISlider.

UISlider当值在其中间位置时,存在三个条件,则原始图像将是其原始位置。
第二种情况,当滑块值最大时,图像将旋转 45 度添加位标度。
第三种情况,当滑块值处于其最小位置时,图像将沿相反方向旋转 45 度,并且还有位标度。
我使用了这段代码,但没有得到想要的结果。

-(IBAction)sliderMoved:(id)sender
{
    UIImage *image = [UIImage imageNamed:@"landscape.jpg"]; 
    photoImage.transform = CGAffineTransformMakeRotation(slider.value * 2*M_PI_2 / slider.maximumValue);
    CGFloat scale=0.5;
    NSLog(@"sliderVlaue=%f",slider.value);
    if ((slider.value) >(0.75)) {
        scale =(.25+ slider.value);
    } else {
        scale =(0.75 +slider.value);
    }
    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [photoImage setTransform:newTransform];
}
4

4 回答 4

0

您可以使用此代码进行图像旋转

imageview.transform = CGAffineTransformMakeRotation(M_PI);

用于缩放图像

-(IBAction)grow
{   
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationRepeatAutoreverses:YES];
    CGRect b = grower.bounds;
    b.size.height = 200;
    b.size.width = 200;
    grower.bounds =  b;
    [UIView commitAnimations];
}
于 2013-09-11T09:24:54.443 回答
0

你可以试试这段代码

CGFloat Angle=(((int)slider.value*M_PI)/180);
transform=CGAffineTransformScale(transform, photoImage.scale, photoImage.scale);
transform= CGAffineTransformRotate(transform,Angle);
photoImage.transform = transform;
于 2013-09-11T09:08:18.857 回答
0

我在给定提示的帮助下尝试了自己,并能够摆脱我的问题。这是代码`

in you h file declare  UIImageView *photoImage;
UIView *canvas;

CAShapeLayer *_marque;

并且在视图中确实加载了

if (!_marque) {
    _marque = [CAShapeLayer layer] ;
    _marque.fillColor = [[UIColor clearColor] CGColor];
    _marque.strokeColor = [[UIColor grayColor] CGColor];
    _marque.lineWidth = 1.0f;
    _marque.lineJoin = kCALineJoinRound;
    _marque.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
    _marque.bounds = CGRectMake(photoImage.frame.origin.x, photoImage.frame.origin.y, 0, 0);
    _marque.position = CGPointMake(photoImage.frame.origin.x + canvas.frame.origin.x, photoImage.frame.origin.y + canvas.frame.origin.y);
}
[[self.view layer] addSublayer:_marque];

    - (IBAction) sliderMoved:(id)sender
     {

    CGFloat scale=0;
   NSLog(@"sliderVlaue=%f",slider.value);
   if ((slider.value) >(0.77)) {
    scale =(.5+ slider.value);
    photoImage.transform = CGAffineTransformMakeRotation(((4*slider.value)*(M_PI/180)));
    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale-.25, scale-.25);

    [photoImage setTransform:newTransform];

    NSLog(@"max=%f",scale);


}else if(slider.value< 0.77 && slider.value >.73){
   photoImage.transform=CGAffineTransformIdentity;
    photoImage.frame=imageframe;
     scale =(0.5 +slider.value);
    NSLog(@"mid=%f",scale);
}
else{
    scale =(.77 +(1.0-slider.value));
    photoImage.transform = CGAffineTransformMakeRotation((((-4)*(slider.value))*(M_PI/180)));
    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [photoImage setTransform:newTransform];
     NSLog(@"min=%f",scale);

}


    //_lastScale = 1;
[self showOverlayWithFrame:photoImage.frame];
}

-(void)showOverlayWithFrame:(CGRect)frame {

if (![_marque actionForKey:@"linePhase"]) {
    CABasicAnimation *dashAnimation;
    dashAnimation = [CABasicAnimation animationWithKeyPath:@"lineDashPhase"];
    [dashAnimation setFromValue:[NSNumber numberWithFloat:0.0f]];
    [dashAnimation setToValue:[NSNumber numberWithFloat:15.0f]];
    [dashAnimation setDuration:0.5f];
    [dashAnimation setRepeatCount:HUGE_VALF];
    [_marque addAnimation:dashAnimation forKey:@"linePhase"];
}

_marque.bounds = CGRectMake(frame.origin.x, frame.origin.y, 0, 0);
_marque.position = CGPointMake(frame.origin.x + canvas.frame.origin.x, frame.origin.y + canvas.frame.origin.y);

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, frame);
[_marque setPath:path];
CGPathRelease(path);
 _marque.hidden = NO;    
  }
于 2013-09-11T11:45:43.417 回答
0

您可以使用以下代码执行此操作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    imgToRotate = [[UIImageView alloc]initWithFrame:CGRectMake(48, 208, 240, 240)];
    imgToRotate.image = [UIImage imageNamed:@"rtm.jpeg"];
    [self.view addSubview:imgToRotate];  
}


- (IBAction)slideToRotateScale:(UISlider *)sender
{
    sender.maximumValue = 1.0;
    sender.minimumValue = -1.0;
    imgToRotate.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(fabsf(sender.value), fabs(sender.value)), sender.value*M_PI/4);
}
于 2013-09-11T09:58:47.017 回答