代码改变世界

ios中图片旋转

2013-08-27 08:53  甘超波  阅读(23798)  评论(0编辑  收藏  举报
@interface ViewController ()
{
    UIImageView *_imageview;
    BOOL flag;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame=CGRectMake(0, 200, 200, 50);
    UIImageView *imgview=[self ImageViewImageName:@"arrow_down" frame:CGRectMake(0, (50-30)*0.5f, 27, 30)];
    [btn addSubview:imgview];
    _imageview=imgview;
    [self.view addSubview:btn];
    flag=YES;
}


- (UIImageView *)ImageViewImageName:(NSString*)aImageName frame:(CGRect)aRect{
    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:aRect] autorelease];
    imageView.userInteractionEnabled = YES;
    UIImage *aImage = [UIImage imageNamed:(aImageName)];
    if ([aImage respondsToSelector:@selector(resizableImageWithCapInsets:)]) {
        imageView.image = [aImage resizableImageWithCapInsets:UIEdgeInsetsMake(aImage.size.height/2, aImage.size.width/2, aImage.size.height/2, aImage.size.width/2)];
    } else {
        imageView.image = [aImage stretchableImageWithLeftCapWidth:aImage.size.width/2 topCapHeight:aImage.size.height/2];
    }
    return imageView;
}



- (IBAction)click:(id)sender {
   
    if (flag) {
        _imageview.transform=CGAffineTransformMakeRotation(M_PI*2);
        flag=NO;
    }
    else{
        _imageview.transform=CGAffineTransformMakeRotation(M_PI*3.5);
        flag=YES;
    }
    
}
@end