To set Uianimation and Touch Event for UIImageview - ios

I'm Working in a project it need to animate the image from one place to another i complete that but my problem is while i animating i don't get the touch event from that UIImageview .so any know please give the solution asap.
- (void) imageSpawn
{
NSArray *images= [NSArray arrayWithObjects:[UIImage imageNamed:#"fish_right1.png"],
[UIImage imageNamed:#"fish_right2.png"], [UIImage imageNamed:#"fish_right3.png"], [UIImage imageNamed:#"fish_right4.png"], [UIImage imageNamed:#"fish_right14.png"], [UIImage imageNamed:#"fish_right15.png"], [UIImage imageNamed:#"fish_right20.png"], nil];
int currentImageIndex=0;
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self.first_fish setImage:[images objectAtIndex:currentImageIndex] ];
}completion:Nil ];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];
}
-(void)ballTapped:(UIGestureRecognizer *)gesture
{
//here also you can get the tapped point if you need
CGPoint location = [gesture locationInView:gesture.view];
NSLog(#"LOCA X:%d",gesture.view.tag);
NSLog(#"LOCA y:%f",location.y);
}
Regards,
Raja.I

There's an option, UIViewAnimationOptionAllowUserInteraction, that you can pass to the options parameter in animateWithDuration:delay:options:animations:completion:. You need to set that to allow interactions during an animation.
After Edit: This has to do with the nature of the way you're doing the animation. If you click on the place where your image view ends up (while the animation is running) you will see that the gesture recognizer fires. In effect, the location of the view is already set to the final value when the animation starts.
To make this work, I think you have to do it with a timer instead of animateWithDuration. Create a repeating timer, and increment the x position of your view with each call, and invalidate the timer when you reach the destination. This will allow you to interact with the view while it moves.

When you animate a view you set the view in its final state. This way you can only detect touches in the final position where you are animating.
However, it is possible to go around that issue. You need to catch the touch event and compare with the presentationLayer.
-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event {
CGPoint point = [[touches anyObject] locationInView:self.view];
if([self.cloudAnimate.layer.presentationLayer hitTest:point]) {
//do something
}
}
The presentation layer has information about the visual position of the CALayer that is associated with your cloudAnimate.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if([imageView1.layer.presentationLayer hitTest:touchLocation]){
// TODO
}
}

Related

iOS: Change alpha of image view with gestures

I want to change the alpha of image with the gestures like up and down or right and left.
I don't want to use slider for that.
Is it possible to do this if yes then please tell me how or i have to use slider for that?
Thank you.
You can implement method on view or view controller
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
NSLog(#"location: %#",NSStringFromCGPoint(location));
// Example:
// alpha depends on location.x related to view bounds width
CGFloat alpha = (self.bounds.size.width - location.x) / self.bounds.size.width;
NSLog(#"alpha: %#",#(alpha));
}
where you can check location.x or location.y.
Try to do like following :
First attach tap GestureRecognizer to view:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(yourTapMethod:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tap];
Then, in gesture recognizer handler do what do you want:
-(void)yourTapMethod{
[UIView animateWithDuration:0.5 animations:^(void){
imageView.alpha = //add any value (like 0.2f);
}

TouchesMoved only moves a little

I am trying to allow the user to drag a label around the screen, but in the simulator it only moves a little each time I touch somewhere on the screen. It will jump to the location and then drag slightly, but then it will stop dragging and I have to touch a different location to get it to move again. Here is my code in my .m file.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Drag = [[event allTouches] anyObject];
firstInitial.center = [Drag locationInView: self.view];
}
My ultimate goal is to be able to drag three different labels on the screen, but I'm just trying to tackle this problem first. I would greatly appreciate any help!
Thanks.
Try using a UIGestureRecognizer instead of -touchesMoved:withEvent:. And implementing something similar to the following code.
//Inside viewDidLoad
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(dragonMoved:)];
panGesture.minimumNumberOfTouches = 1;
[self addGestureRecognizer:panGesture];
//**********
- (void)dragonMoved:(UIPanGestureRecognizer *)gesture{
CGPoint touchLocation = [gesture locationInView:self];
static UIView *currentDragObject;
if(UIGestureRecognizerStateBegan == gesture.state){
for(DragObect *dragView in self.dragObjects){
if(CGRectContainsPoint(dragView.frame, touchLocation)){
currentDragObject = dragView;
break;
}
}
}else if(UIGestureRecognizerStateChanged == gesture.state){
currentDragObject.center = touchLocation;
}else if (UIGestureRecognizerStateEnded == gesture.state){
currentDragObject = nil;
}
}

Add an image to touch location iOS

I have an app that connects points in an array with lines, what I want to do next is to add a marker to every line's end points, I chose a red dot for that. But I can't figure out how to even start. The image should appear every time the user taps the screen, because that's when a new point is created. Thanks.
The simplest solution would be to add a UITapGestureRecognizerto your view with the following method as the GR's action. Supposing you're using e.g an image asset for your dot it would be:
-(void)screenTapped:(UITapGestureRecognizer*)tap {
CGPoint tapLocation = [tap locationInView:self.view];
UIImageView *redDotImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"redDot"]];
redDotImageView.center = tapLocation;
[self.view addSubview:redDotImageView];
}
For example you can add gesture recognized to your view:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addDotToTapPoint:)];
[view addGestureRecognizer:tapGesture];
then implement method addDotToTapPoint: to find point of tap:
- (void)addDotToTapPoint:(UITapGestureRecognizer *)sender {
CGPoint pointOfTouch = [sender locationInView:sender.view];
//... draw circle or something
}
Second approach.
You can create your own subclass of UIView and implement method touchesBegan:withEvent:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pointOfTouch = [(UITouch *)[touches anyObject] locationInView:self];
//... draw circle or something
}
In both cases You will get location of touch and can draw something in that point

Define a rectangle of locations in objective-C

I have an Image in an Iphone App, I would like when a user taps a location within a defined area (range of locations or an area defined by the rectangle), it triggers another event. I know how to get a single location, But I don't know how to define a rectangular area. I am looking for a simple way to implement it. Thank you
If you want to make the rectangle visible you can add image view to your view and set up the tap recogniser. But if you don't want to make the rectangle visible you can override touchesBegan:withEvent: method and use CGRectContainsPoint:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
CGRect rect = CGRectMake(0.0, 0.0, 100, 100); //<- this is the rectangle you do check on
if (CGRectContainsPoint(rect, touchLocation)) {
NSLog(#"You tapped inside rectangle");
}
else {
NSLog(#"You missed rectangle");
}
}
Define your area as a CGRect and get the CGPoint of the touch. Then use CGRectContainsPoint to check for a hit.
If you need to create the CGRect from a list of points then you need to iterate the points and find the max and min x and y values, then you can create the CGRect with:
CGRectMake(minX, minY, maxX - minX, maxY - minY);
Maybe you can create a UIView and add a gesture recognizer like this:
UIView *customView = [UIView new];
customView.frame = CGRectMake(....); // as Wain suggest
[self.view addSubview:customView];
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget: self
action: #selector(someMethod)];
[customView addGestureRecognizer:tapGesture];

How to move to the next screen when the image drops onto when I tap the button?

I wrote the animation concept in viewDidLoad method i wrote:
scaleFactor=2;
angle=180;
CGRect frame = CGRectMake(10,10,45,45);
UIView *box;
box=[[UIView alloc] initWithFrame:frame];
box.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"images673.png"];
[self.view addSubView:box];
// This code is for the touch event of an object in the screen
// For the object movement i wrote the method which is shown below:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:self.view];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UICiew setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
box.center=location;
[UIView commitAnimations];
}
// And then,now i wrote a button which has to navigate to another class file// General navigation principle i wrote
-(IBAction)settings:(id)sender
{
aScreen *abc=[[aScreen alloc]init];
[self.navigationController pushViewController:abc animated:YES];
[abc release];
}
Now the object is moving where i tap on the screen,but when i tap on the button,the image is not placing on the top of the button.What to do.Please help me.I took an image and then I made some animation for it to move till where I tap in the screen. But at the same time, I took a button and I wrote a button event action in viewcontroller. When I tap the button, the image is not moving onto the button.
What I want to do is if I tap the button, the image has to drop onto the button and it should display the next view controller. Can anyone help me in doing so? How to move to the next screen when the image drops onto when I tap the button?
Im not sure i fully understand the problem without seeing your code but cant you do something like this?
-(ibaction)buttonevent:(id)sender{
cgrect frame = image.frame;
frame.origin.x = button.frame.x;
frame.origin.y = button.frame.y;
image.frame = frame;
image.alpha = 0;
[self pushviewcontroller:newViewcontroller animated:yes];
}
hi try this code i have checked. This is working for me when you check for button and the image you are dragging intersect then fire the method where it moves to next screen
// Handles the continuation of a touch.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(#"touchesMoved");
UITouch *touch = [touches anyObject];
if ([touch view] == mKey) {
NSUInteger touchCount = 0;
// Enumerates through all touch objects
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self.view]];
touchCount++;
}
}
}
// Checks to see which view, or views, the point is in and then sets the center of each moved view to the new postion.
// If views are directly on top of each other, they move together.
-(void)dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position
{
// Check to see which view, or views, the point is in and then move to that position.
if (CGRectContainsPoint([mKey frame], position))
{
NSLog(#"touchesMoved");
mKey.center = position;
}
if(CGRectIntersectsRect([mKey frame], [inVisible frame]) && k==0)
{
self.view.userInteractionEnabled=NO;
NSLog(#"thirdPiew frame");
[inVisible setHighlighted:YES];
[inVisible setShowsTouchWhenHighlighted:YES];
k=1;
[self performSelector:#selector(modalPresentationButtonPressed:)];
}
}

Resources