Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make an image follow your finger in objective-c? [closed]

How do you make an image follow your finger in objetive-c?

like image 671
Joseph800 Avatar asked Aug 10 '10 01:08

Joseph800


2 Answers

  • Create your UIView and configure it (or any subclass thereof such as a UIImageView)
  • Set the position of your Image to be where the user is touching:

There are four delegate methods for accepting touch events which are a part of any class that inherits from UIResponder such as a UIView. Use the delegate method that is most appropriate to you. If you want it to follow your finger, this will be -touchesMoved:

- (void) touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event {        
    CGPoint pt = [[touches anyObject] locationInView:self];
    myImageView.center = pt;
}

Other delegate methods available to you are:

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

I wrote an example application that does exactly what you want. It's a demo of drawing Quartz 2D graphics, but it draws a red square and black circle where you drag your finger and should be simple enough to follow:

alt text http://brockwoolf.com/shares/stackoverflow/3445494/screen.png

Download Xcode project (32kb)

like image 80
Brock Woolf Avatar answered Sep 20 '22 01:09

Brock Woolf


there is an exellent post here.

by Divan Visagie

here is the relevant code (taken from the link above):

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    //Find the path for the menu resource and load it into the menu array
    NSString *menuPlistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];

    menuArray = [[NSArray alloc] initWithContentsOfFile:menuPlistPath];

    //add some gestures
//    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
//    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    //[self.view addGestureRecognizer:swipeLeft];

//    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
//    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    //[self.view addGestureRecognizer:swipeRight];

}



float difference;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint contentTouchPoint = [[touches anyObject] locationInView:content];
    difference = contentTouchPoint.x;
}


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

    CGPoint pointInView = [[touches anyObject] locationInView:self.view];

    float xTarget = pointInView.x - difference;
    if(xTarget > menuTable.frame.size.width)
        xTarget = menuTable.frame.size.width;
    else if( xTarget < 0)
        xTarget = 0;

    [UIView animateWithDuration:.25
                     animations:^{

                         [content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
                     }
     ];
}


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


    CGPoint endPoint = [[touches anyObject] locationInView:self.view];
    float xTarget = endPoint.x - difference;
    if(xTarget < (menuTable.frame.size.width/2))
        xTarget = 0;
    else
        xTarget = menuTable.frame.size.width;

    [UIView animateWithDuration:.25
                     animations:^{

                         [content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
                     }
     ];
}
like image 39
George Asda Avatar answered Sep 20 '22 01:09

George Asda