UIPinchGestureRecognizer 缩放,移动,旋转,UIImagePickerController 横屏,全屏 的实现

@interface PhotographViewController : UIViewController  {
    UIImagePickerController *imagePicker;
    UIImage *photoImage;
    UIView *overlay;
    UIImageView *photoCar;
    UIView *viewPhoth;
    
    
    CGFloat lastScale ;
    UIImageView  *outPhotoImage;
}
@property (nonatomic,retain) IBOutlet UIImagePickerController *imagePicker;
@property (nonatomic,retain) IBOutlet UIView *overlay;
@property (nonatomic,retain) IBOutlet UIImageView  *outPhotoImage;
@property (nonatomic,retain) IBOutlet UIImageView *photoCar;
@property (nonatomic,retain) IBOutlet   UIView *viewPhoth;
-(void)performAction:(id)sender;
-(IBAction)returnView;
-(IBAction)photographAction;
-(IBAction)photographActionImage;

-(void)scale:(UIPinchGestureRecognizer *)sender;
- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer;
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer;
@end

上面是头文件就要加
UIGestureRecognizerDelegate

协议


。M

//
//  PhotographViewController.m
//  test1
//
//  Created by Mica001 on 11-8-30.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "PhotographViewController.h"


@implementation PhotographViewController
@synthesize imagePicker;
@synthesize overlay;
@synthesize outPhotoImage;
@synthesize photoCar;
@synthesize viewPhoth;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [viewPhoth release];
    [photoCar release];
    [outPhotoImage release];
    [overlay release];
    [imagePicker release];
    [super dealloc];
}




-(IBAction)returnView
{
     [self dismissModalViewControllerAnimated:YES] ;
}

-(IBAction)photographAction
{
    self.imagePicker.sourceType= UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.showsCameraControls = NO;
    self.imagePicker.cameraOverlayView=self.overlay;
 //这里放大,把工具栏隐藏,  在
//overlay  里手工添加一些控制button  当这,这里要用imageview 模拟成button
   imagePicker.cameraViewTransform = cameraTransform; 
    [self presentModalViewController:imagePicker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{
   
  NSLog(@"image");
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
{
    photoImage=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    outPhotoImage.image=photoImage;
    [picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
   NSLog(@"s");
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    lastScale=1.0;
    UIImage *image1=[UIImage imageNamed:@"dzlogo.png"];
    UIImageView *imageViewCarS1= [[UIImageView alloc] initWithImage:image1];
    imageViewCarS1.frame=CGRectMake(0, 0, imageViewCarS1.frame.size.width * 0.4, imageViewCarS1.frame.size.height * 0.4)   ;
    [imageViewCarS1 setTag:11];
    self.navigationItem.titleView=imageViewCarS1 ;
    [imageViewCarS1 release];  
    
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc] initWithTitle:@"选项" style:UIBarButtonItemStylePlain target:self action:@selector(performAction:)] autorelease];
    
    
     // 放大缩小手势
    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] 
                                                 
                                                 initWithTarget:self action:@selector(scale:)];
    [pinchRecognizer setDelegate:self];
    [self.overlay addGestureRecognizer:pinchRecognizer];
    [pinchRecognizer release];    
    
   // 旋转手势
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotatePiece:)];
    [self.viewPhoth addGestureRecognizer:rotationGesture];
    [rotationGesture release];    
    
    
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
    
    [panGesture setMaximumNumberOfTouches:1];
    
    [panGesture setDelegate:self];
    
    [viewPhoth addGestureRecognizer:panGesture];
    
    [panGesture release];


}

-(void)scale:(UIPinchGestureRecognizer*)sender {
    
    //当手指离开屏幕时,将lastscale设置为1.0
    if([sender state] == UIGestureRecognizerStateEnded) {
        lastScale = 1.0;
        return;
    }
    
    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
    CGAffineTransform currentTransform = self.photoCar.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    
    [self.photoCar setTransform:newTransform];
    lastScale = [sender scale];
    
}

- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer
{
     
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
       // rotate = [gestureRecognizer rotation];
        [gestureRecognizer setRotation:0];
    }
}

- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer

{
    
    UIView *piece = [gestureRecognizer view];
    
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        
        CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
        
        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
        
        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
        
    }
    
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
            ||(interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

-(void)performAction:(id)sender;
{
   
}

-(IBAction)photographActionImage
{
    [self.imagePicker takePicture];
}
@end


 
  


你可能感兴趣的:(objective,c)