2013-01-03 22 views
0

我想使用UITouch移動鉛筆圖像?我想移動鉛筆,它會寫我們想要的任何東西。如何使用UITouch移動鉛筆圖像?

但我不能移動和寫入鉛筆圖像。

來自專家的任何提示都會受到歡迎。

回答

0

使用下列方法:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self]; 
    yourPencilImgView.center = [touch locationInView:self]; 
} 

而且

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self]; 
    yourPencilImgView.center = currentPoint; 
} 
0

您可以使用UIPanGestureRecognizer

- (void)onDraggingPencil:(UIPanGestureRecognizer *)panGR { 

    CGPoint translation = [panGR translationInView:baseView]; 

    if (panGR.state == UIGestureRecognizerStateBegan) { 

    } else if (panGR.state == UIGestureRecognizerStateChanged) { 

     CGRect _rect = panGR.view.frame; 
     _rect.origin.x = dragPoint.x + translation.x; 
     _rect.origin.y = dragPoint.y + translation.y; 
     panGR.view.frame = _rect; 

    } else if (panGR.state == UIGestureRecognizerStateEnded) { 

    } 
} 
0

下面的代碼是使用了移動圖片上的觸摸

AppDelegate Classes 

**// .h File** 

#import <UIKit/UIKit.h> 

@class UITouchTutorialViewController; 

@interface UITouchTutorialAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    UITouchTutorialViewController *viewController; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UITouchTutorialViewController *viewController; 

@end 

////////////////////////////////////////////////////////////////////////////////// 

// .m File 

#import "UITouchTutorialAppDelegate.h" 
#import "UITouchTutorialViewController.h" 

@implementation UITouchTutorialAppDelegate 

@synthesize window; 
@synthesize viewController; 


- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    // Override point for customization after app launch  
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 


- (void)dealloc { 
    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 


@end 
////////////////////////////////////////////////////////////////////////////// 

UIViewController Classes 

// .h file 

#import <UIKit/UIKit.h> 

@interface UITouchTutorialViewController : UIViewController { 
    IBOutlet UIImageView *cloud; 
} 

@end 

// .m File 

#import "UITouchTutorialViewController.h" 

@implementation UITouchTutorialViewController 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 
    cloud.center = location; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self touchesBegan:touches withEvent:event]; 
} 

/* 
// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically. 
- (void)loadView { 
} 
*/ 


/* 
// Implement viewDidLoad to do additional setup after loading the view. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 


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


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 
0

您可以將使用以下代碼下降的圖像

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
[panRecognizer setMinimumNumberOfTouches:1]; 
[panRecognizer setMaximumNumberOfTouches:1]; 
[panRecognizer setDelegate:self]; 
[imageView_ addGestureRecognizer:panRecognizer]; 

- (無效)移動:(ID)發送方{

CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view]; 

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 

    firstX = [[sender view] center].x; 
    firstY = [[sender view] center].y; 
} 

translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y); 
[[sender view] setCenter:translatedPoint]; 

}