I have to find Endpoint from start and moved point.
I am doing animation and I need to move View when user drag the view then I have to through it out of the screen and bring back to the original point.
Right now I have used UISwipeGestureRecognizer for detecting swipe on Move. Following is the code.
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
// Adding the swipe gesture on image view
[_view1 addGestureRecognizer:swipeLeft];
[_view1 addGestureRecognizer:swipeRight];
[_view1 addGestureRecognizer:swipeUp];
[_view1 addGestureRecognizer:swipeDown];
Handling Swipe
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
CGPoint movedPoint = [swipe locationInView:swipe.view];
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"Left Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find END POINT BASED ON THIS DATA
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(#"Right Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(#"Up Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(#"Down Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
}
When I swipe No.1 view then I can get moved point in SwipeHandler (handleSwipe) Method
So I can also detect Direction of Swipe. But my problem is that I have to through No.1 View out of the screen. For that I have to find the Endpoint.
So, how can I find Endpoint from Starting point and MovedPoint ?
Here is the link fro the endpoint calculation. http://library.thinkquest.org/20991/geo/coordgeo.html
Related
In my Xib. I have included 3 sub views and 3 small imageviews
![I Have to include animation to right swipe and left swipe for each view separately. Simple swipe for one view works fine according to the direction of swipe the item gets Favorited and the image position also could change ][2]
//........towards right Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightRecognizer];
//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftRecognizer];
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"Right Gesture");
self.Favimage.hidden = false;
self.closeImage.hidden = true;
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"Left Gesture");
self.Favimage.hidden = true;
self.closeImage.hidden = false;
}
This what im tried now. I have to do it with better animation. Thanks in advance
i think. This is what you are looking for
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"Right Gesture");
self.closeImage.hidden = true;
// self.shoe.hidden = true;
//Do moving
CGRect finalFrame = CGRectMake(150,5, 64, 63);
[UIView animateWithDuration:0.5 animations:^{
_shoe.frame = finalFrame;
self.Favimage.hidden = true;
} completion:^(BOOL finished) {
self.Favimage.hidden = false;
// [_Favimage removeFromSuperview];
//[_Favimage removeFromSuperview];
//[self.view removeGestureRecognizer:gestureRecognizer];
}];
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"Left Gesture");
self.Favimage.hidden = true;
self.closeImage.hidden = false;
CGRect finalFrame = CGRectMake(77,5, 70, 63);
[UIView animateWithDuration:0.5 animations:^{
_shoe.frame = finalFrame;
} completion:^(BOOL finished) {
//self.Favimage.hidden = false;
//[_Favimage removeFromSuperview];
//[_Favimage removeFromSuperview];
//[self.view removeGestureRecognizer:gestureRecognizer];
}];
// do moving
}
try this one
Hello and thank you in advance...
I am moving views frame location in animation blocks. After I move views, I am tapping a textView to allow for editing/input. When the textView is tapped, the views I moved previously pop back to their original (unmoved) frame locations.
I'm not sure what I am doing incorrectly here.
THIS IS HOW I SETUP THE DIFFERENT FRAME POSITIONS TO ANIMATE TO
//Books active and rest position will effect other elements (paper and paperTextView)
self.bookRestPosition = CGRectMake((self.view.frame.size.width * 0.10), (self.view.frame.size.height * 0.4), self.view.frame.size.width, self.view.frame.size.height);
self.bookActivePosition = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y +100, self.view.frame.size.width, self.view.frame.size.height);
self.bookOpenPosition = CGRectMake(self.bookActivePosition.origin.x, self.bookActivePosition.origin.y, 20, self.bookActivePosition.size.height);
self.quillRestPosition = self.quill.frame;
self.quillActivePosition = CGRectMake(self.quill.frame.origin.x, self.view.frame.origin.y -200, self.quill.frame.size.width, self.quill.frame.size.height);
self.woodTableRestPosition = self.view.frame;
self.woodTableActivePosition = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -200, self.view.frame.size.width, self.view.frame.size.height +200);
self.paperRestPosition = self.bookRestPosition;
self.paperActivePosition = CGRectMake(self.bookOpenPosition.size.width, self.bookActivePosition.origin.y, self.bookActivePosition.size.width, self.bookActivePosition.size.height);
self.paperTextViewRestPosition = self.bookRestPosition;
self.paperTextViewActivePosition = self.paperActivePosition;
self.paperTextView.editable = NO;
self.paper.alpha = 0.0;
self.paperTextView.alpha = 0.0;
self.paper.frame = self.bookRestPosition;
self.paperTextView.frame = self.bookRestPosition;
THIS IS HOW I ANIMATE THEM
- (IBAction)onBookTapped:(UITapGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == self.bookRestPosition.origin.x) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self activateLogAndViewsAssociated];
} completion:^(BOOL finished) {
NSLog(#"Book Slid up!");
}];
}
else if (self.logCover.frame.origin.x == 0){
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self everythingAtRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
}
- (IBAction)swipeBookLeft:(UISwipeGestureRecognizer *)sender {
//if (self.logCoverImage.frame.origin.x == 0) {
self.paper.alpha = 1.0;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookOpenPosition];
self.editButton.alpha = 1.0;
self.editButton.enabled = YES;
} completion:^(BOOL finished) {
NSLog(#"Book Has Opened!");
//This must be here. The paperTextView is what the swipe right gesture is attached to
self.paperTextView.alpha = 1.0;
// self.logCoverImage.alpha = 0.0;
// self.quill.alpha = 0.0;
}];
//}
}
- (IBAction)onPaperSwipeRight:(UISwipeGestureRecognizer *)sender {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookActivePosition];
self.editButton.alpha = 0.0;
self.editButton.enabled = NO;
} completion:^(BOOL finished) {
NSLog(#"Book Has Closed!");
//self.paper.alpha = 0.0;
// [NSUserDefaults *log = [NSUserDefaults standardUserDefaults];
// [log setObject:self.paperTextView forKey:#"textLog"];
}];
}
- (IBAction)onBookSwipeDown:(UISwipeGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == 0) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self everythingAtRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
}
- (IBAction)onBookSwipeUp:(UISwipeGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == self.bookRestPosition.origin.x) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self activateLogAndViewsAssociated];
} completion:^(BOOL finished) {
NSLog(#"Book Slid up!");
}];
}
}
- (void)animateAtStartup
{
self.logCover.frame = CGRectMake(self.view.frame.size.width, self.view.frame.size.height, self.bookRestPosition.size.width, self.bookRestPosition.size.height);
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
AND THIS IS WHAT HAPPENS WHEN THE EDIT BUTTON IS PRESSED
- (IBAction)onEditButtonPressed:(id)sender
{
self.paperTextView.editable = YES;
[self.paperTextView becomeFirstResponder];
}
I have touch event animations of 'clouds' using this code. All working fine but I want to fade/remove clouds after the user taps the cloud 3 times. So I want them to disappear after the third time it is touched. How do I do this?
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
CGRect cloudBLRect = [[[self.cloudBL layer] presentationLayer] frame];
if (CGRectContainsPoint(cloudBLRect, touchLocation)) {
NSLog(#"cloudBL tapped!");
cloudBLPressed = true;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(200, 600);
self.cloudBL.alpha = 0.5;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:2.0
delay:2.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(100, 700);
self.cloudBL.alpha = 0.5;
} completion:^(BOOL finished) {
self.cloudBL.alpha = 1.0;
}];
} else {
NSLog(#"cloud not tapped.");
return;
}
if (cloudBLPressed) return;
}
Take a variable count and initialize it to 0. On each touch, increment it by 1. Also check in the touchGesture method, that if the count variable equals to 2 then set alpha for the cloud to 0.0.
Something like this:
in .m file take a private int vairiable: int count;
in viewDidLoad: count = 0; cloudView.alpha = 1.0;
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
count++;
if(count<2)
{
cloudView.alpha-=0.33;
}
else {
cloudView.alpha = 0.0;
}
}
Add it in your animation logic. Hope that helps.
You can set it in your code like this:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
CGRect cloudBLRect = [[[self.cloudBL layer] presentationLayer] frame];
if(count < 2)
{
count++;
if (CGRectContainsPoint(cloudBLRect, touchLocation)) {
NSLog(#"cloudBL tapped!");
cloudBLPressed = true;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(200, 200);
// self.cloudBL.alpha -=0.33;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:2.0
delay:2.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(100, 300);
self.cloudBL.alpha -=0.33;
} completion:^(BOOL finished) {
// self.cloudBL.alpha = 1.0;
}];
}];
}
else {
NSLog(#"cloud not tapped.");
return;
}
if (cloudBLPressed) return;
} else {
[UIView animateWithDuration:2.0
delay:2.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(100, 300);
self.cloudBL.alpha =0.0;
} completion:^(BOOL finished) {
}];
}
}
You can do that:
UITapGestureRecognizer * tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(yourAction:)];
tripleTap.numberOfTapsRequired = 3;
[yourView addGestureRecognizer:tripleTap];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnImage:)];
[cloud addGestureRecognizer:tapRecognizer];
}
- (void)tapOnImage:(UITapGestureRecognizer *)gesture
{
tapsCounter++;
if (tapsCounter == 3)
{
// do your stuff
tapsCounter = 0;
}
}
I would suggest you to move this logic into UIImageView subclass that represents the cloud object.
Add the proper delegate UIGestureRecognizerDelegate to your interface.
Then in your viewDidLoad:
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMethod)];
tapped.delegate=self;
tapped.numberOfTapsRequired = 3;
[self.view addGestureRecognizer:tapped];
and then in your View Controller:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view != cloudView&& cloudView)
{
return YES;
}
return NO;
}
-(void)tapMethod
{
[cloudView removeFromSuperview];
cloudView = nil;
}
I've made a hint box that slides up with an animation, but i also want to -10 coins when the user taps on a candle button so I created a series of photos that make an animation which depicts -10 to the user...
the problem is that every time the candle button is pressed the -10coins animation overrides the hint sliding box and replaces it, thus making it impossible for the user to find a hint again...
Code:
- (IBAction)firstHintq:(id)sender {
[_hintView setHidden:NO];
[_hintViewTwo setHidden:YES];
[_hintViewThree setHidden:YES];
_hintView.text = #"Ηταν η τεταρτη τηλεφωνικη εταιρια στην Ελλαδα";
//connected to global BOOL
if (!btn1Pressed) {
if((coins -10) >= 0){
coins = coins -10;
score = score -2;
// Load images starts
NSArray *imageNames = #[ #"coin-10-1.png", #"coin-10-2.png",
#"coin-10-3.png", #"coin-10-4.png", #"coin-10-5.png", #"coin-10-6.png",
#"coin-10-7.png", #"coin-10-8.png", #"coin-10-9.png", #"coin-10-10.png", #"coin-10-11.png",#"coin-10-12.png", #"coin-10-13.png",#"coin-10-14.png",#"coin-10-15.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 200, 30, 70)];
animationImageView.animationImages = images;
animationImageView.animationDuration = 1.5;
animationImageView.animationRepeatCount = 10;
[self.view addSubview:animationImageView];
[animationImageView startAnimating];
//coin animation ends
//changes image
[_candleone setImage:[UIImage imageNamed:#"candle2_03.png"] forState:UIControlStateNormal];
self.candletwo.hidden = NO;
//Animates the 2nd candle
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGPoint center = [_candletwo center];
center.x = 88;
center.y = 70;
[_candletwo setCenter:center];
[UIView commitAnimations];
//sets the global BOOL as true
coinsLabel.text = [NSString stringWithFormat:#"%d",coins];
btn1Pressed = true;
}
else{
[_hintView setHidden:YES];
//Show an alert that the user has not enough coins
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE" message:#"MESSAGE" delegate:nil cancelButtonTitle:#"RETURN BUTTON" otherButtonTitles:nil];
[alert show];
}
}
}
How to fix this issue, so that both animations activate properly?
For animations better use:
[UIView animateWithDuration:time // time is a double
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^() {
}
completion:^(BOOL finished) {
}];
Then you can use 2 animateWithDuration: delay: options: completion:
And OFC, you can nest 2:
[UIView animateWithDuration:time // time is a double
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^() {
}
completion:^(BOOL finished) {
[UIView animateWithDuration:time // time is a double
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^() {
}
completion:^(BOOL finished) {
}];
}];
I create a UIView and set it's layer's contents a image. And I added a tap gesture to this view. When I tap the view, I want it to rotate 180 degree, and when I click close button, I want it to rotate to it's original degree. But it won't work when I try to rotate it back.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"menuArray"];
self.menuView.layer.contents = (__bridge id)image.CGImage;
self.menuView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap)];
[self.menuView addGestureRecognizer:tap];
}
//rotate it 180 degree
- (void)tap
{
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseIn animations:^{
self.menuView.layer.affineTransform = CGAffineTransformMakeRotation(M_PI);
} completion:^(BOOL finished) {
}];
}
//rotate it back. this won't work. I have change the value from M_PI to M_PI_2, M_PI_4. it's not working.
- (IBAction)close:(id)sender
{
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseIn animations:^{
self.menuView.layer.affineTransform = CGAffineTransformMakeRotation(-M_PI);
} completion:^(BOOL finished) {
}];
}
To undo an affineTransorm you should set it to the constant affineTransformIdentity , which is tranform of nil so to speak, a matrix of all zeros..
So in your second method where you want to rotate back set self.menuView.layer.affineTransform = CGAffineTransformIdentity