check if pinch is released in UIPinchGestureRecognizer - iphone

How do I check if a pinch has been released? I tried
- (IBAction)resizeImage:(UIPinchGestureRecognizer *)sender
{
if (sender.delaysTouchesEnded)
//here
}
and it didn't work out

You need to check for sender.state == UIGestureRecognizerStateEnded and possibly sender.state == UIGestureRecognizerStateCancelled

UIAdam's answer worked for me...
if([(UIPinchGestureRecognizer *)sender state] == UIGestureRecognizerStateEnded)
{
}
This is my complete method with zoom (CGAffineTransformScale)
(self.pictureCard01 is a UIView subclass which I'm pinching)
- (IBAction)PinchGesture01:(UIGestureRecognizer *)sender {
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
if (factor > 2) {
factor = 2;
}
else if (factor < 1) {
factor = 1;
}
[UIView animateWithDuration:2 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent
animations:^{
self.pictureCard01.transform = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
}
completion:nil];
if([(UIPinchGestureRecognizer *)sender state] == UIGestureRecognizerStateEnded)
{
[self performSelector:#selector(resize:) withObject:self.pictureCard01 afterDelay:2];
}
}
-(void)resize:(UIView*)myview{
[UIView animateWithDuration:2 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent
animations:^{
myview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
}
completion:nil];
}

Related

Orientation is not working in ios6?

I am having problem in Orientation for ios6. same code is working fine for ios5 or 5.1.
I have used - (BOOL) shouldAutorotate and -(NSInteger)supportedInterfaceOrientations as per the ios6 standard. But still "willRotateToInterfaceOrientation" and "didRotateToInterfaceOrientation" is not getting called.
Here is my code:--
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (!UIInterfaceOrientationIsPortrait(lastOrientation) || !UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
if (!UIInterfaceOrientationIsLandscape(lastOrientation) || !UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
CGRect frame;
int viewAlpha;
lastOrientation = toInterfaceOrientation;
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
viewAlpha = 1;
[MovieControlContainerLandscape setHidden:YES];
if (isDauntless) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
} else {
[self.navigationController setNavigationBarHidden:NO];
}
frame = iPad ? CGRectMake(0, 88, 768, 432) : CGRectMake(0, 88, 320, 180);
[movieContainer removeGestureRecognizer:toggleMediaControl];
}
else
{
[PromptToBuy dismissWithClickedButtonIndex:0 animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
viewAlpha = 0;
[MovieControlContainerLandscape.layer setCornerRadius:22];
[MovieControlContainerLandscape.subviews.lastObject addSubview:[MediaControls use]];
if (isDauntless) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else {
[self.navigationController setNavigationBarHidden:YES];
}
frame = iPad ? CGRectMake(0, 0, 1024, 768) : CGRectMake(0, 0, 480, 320);
[movieContainer addGestureRecognizer:toggleMediaControl];
if (isDauntless) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
}
if(TSFullScreen)
{
[movieContainer setAlpha:1];
}
if (isDauntless) {
[UIView commitAnimations];
}
}
[viewContainer setAlpha:viewAlpha];
// Size the overlay view for the current orientation.
[movieContainer setFrame:frame];
}
}
/* Sent to the view controller after the user interface rotates. */
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (!UIInterfaceOrientationIsPortrait(lastOrientation) || !UIInterfaceOrientationIsPortrait(fromInterfaceOrientation))
if (!UIInterfaceOrientationIsLandscape(lastOrientation) || !UIInterfaceOrientationIsLandscape(fromInterfaceOrientation))
{
float duration = .5;
if (fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (isDauntless) {
[UIView transitionWithView:movieContainer duration:duration options:UIViewAnimationOptionTransitionNone animations:^{
/* Move movie view to parent center. */
[self.moviePlayerController.view setCenter:movieContainer.center];
} completion:^(BOOL finished) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
CGRect frame = movieContainer.frame;
/* Size movie view to fit parent view. */
frame.origin.y = 0;
[self.moviePlayerController.view setFrame:frame];
[UIView commitAnimations];
CGPoint center = CGPointMake(movieContainer.center.x, -50);
[MovieControlContainerLandscape setTag:0];
[MovieControlContainerLandscape setHidden:NO];
[MovieControlContainerLandscape setCenter:center];
[self toggleMovieController];
}];
} else {
CGRect frame = movieContainer.frame;
/* Size movie view to fit parent view. */
frame.origin.y = 0;
[self.moviePlayerController.view setFrame:frame];
CGPoint center = CGPointMake(movieContainer.center.x, -50);
[MovieControlContainerLandscape setTag:0];
[MovieControlContainerLandscape setHidden:NO];
[MovieControlContainerLandscape setCenter:center];
[self toggleMovieController];
}
}
else
{
if (!isDauntless) {
[MovieControlContainer setFrame:CGRectMake(0, 44, 768, 68)];
}
[MovieControlContainer addSubview:[MediaControls use]];
//NSLog(#"MovieControlContainer is %#, MovieControlContainer subviews: %#",MovieControlContainer,[MovieControlContainer subviews]);
if (isDauntless) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
}
if(TSFullScreen){
[movieContainer setAlpha:0];
}
CGRect frame = movieContainer.frame;
/* Size movie view to fit parent view. */
frame.origin.y = 0;
[self.moviePlayerController.view setFrame:frame];
if (isDauntless) {
[UIView commitAnimations];
}
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return !mediaTypeIsAudio && isOrientationSupported;
}
//----supported method for ios6--------//
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSInteger)supportedInterfaceOrientations{
NSInteger mask = 0;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight])
mask |= UIInterfaceOrientationMaskLandscapeRight;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeLeft])
mask |= UIInterfaceOrientationMaskLandscapeLeft;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortrait])
mask |= UIInterfaceOrientationMaskPortrait;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortraitUpsideDown])
mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
return mask;
}
//i have set the rootviewcontroller in appdelegate file
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController.
Please help me to take out from this problem. I am not able to find the solution for this.Thanks in Advance !!.
In ios6 you have to use this method for vieworientation
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskAll;
}
And check my answer Link

addSubview animation works only after second time

I am trying to make my label appear with animation:
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
if (isShowingRectangleLabel == NO) {
[UIView transitionWithView:rectangleLabel duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^ { [self.view addSubview:rectangleLabel]; }
completion:nil];
NSLog(#"action");
isShowingRectangleLabel = YES;
} else {
[UIView transitionWithView:rectangleLabel duration:0.5
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^ { [rectangleLabel removeFromSuperview]; }
completion:nil];
isShowingRectangleLabel = NO;
}
}
But this animation works only after second adding to subview. How can I fix it?
EDIT To clarify, addSubview works but without animation.
Do this:
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
if (isShowingRectangleLabel == NO) {
[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^ { [self.view addSubview:rectangleLabel]; }
completion:nil];
NSLog(#"action");
isShowingRectangleLabel = YES;
} else {
[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^ { [rectangleLabel removeFromSuperview]; }
completion:nil];
isShowingRectangleLabel = NO;
}
}

in iOS5 my methods for switching pictures in gallery are bad

I have some classes for watching pictures in gallery. in those classes after enabling ARC i get message that implicit conversion is forbidden by ARC and I cant run the application.
Those methods are:
- (void) curlToPrevious
{
if (currentImageIndex == 0) return;
if ([self.image2 superview] == NO) {
self.image2.image = (UIImage*) [imageViews objectAtIndex:(currentImageIndex-1)];
} else {
self.image1.image = (UIImage*) [imageViews objectAtIndex:(currentImageIndex-1)];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration];
currentImageIndex--;
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.containerView cache:YES];
if ([self.image2 superview] == NO) {
[self.image1 removeFromSuperview];
[self.containerView addSubview:self.image2];
} else {
[self.image2 removeFromSuperview];
[self.containerView addSubview:self.image1];
}
[UIView commitAnimations];
[self updateCurrentImageCounter];
}
- (void) curlToNext
{
if (currentImageIndex == ([self imageCount]-1)) return;
if ([self.image2 superview] == NO) {
self.image2.image = (UIImage*) [imageViews objectAtIndex:(currentImageIndex+1)];
} else {
self.image1.image = (UIImage*) [imageViews objectAtIndex:(currentImageIndex+1)];
}
currentImageIndex++;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.containerView cache:YES];
if ([self.image2 superview] == NO) {
[self.image1 removeFromSuperview];
[self.containerView addSubview:self.image2];
} else {
[self.image2 removeFromSuperview];
[self.containerView addSubview:self.image1];
}
[UIView commitAnimations];
[self updateCurrentImageCounter];
}
I have
if ([self.image2 superview] == NO) {
In this line on 4 places in code problem.
The text I get is:
implicit conversion of 'int' to 'UIView' is disallowed with ARC
How can I avoid this??? Thanks.
you can replace your code on this if ([self.image2 superview] == nil)
You are comparing an UIView* with an integer (0) - of course you get a warning. Do you want to check whether the superview is nil? Then just do that:
if([self.image2 superview] == nil) { ... }

Orientation Keyboard Movement

Here are some great tutorials on portrait textfield movement.
One
Two
Three
My View, on the other hand, rotates to both portrait and landscape, and in both orientations the keyboard obscures the textfield... Right now, both portrait, and One of the landscape orientations work.
So I'm wondering how I can include both landscape orientations.
Here's what I'm doing:
-(void) keyboardWillShow:(NSNotification *)notif{
if ([serverIP isFirstResponder]){
if (isPortrait && self.view.frame.origin.y >= 0){
[self setViewMovedVertical:YES];
}
else if (!isPortrait && self.view.frame.origin.x >= 0){
[self setViewMovedHorizontal:YES];
}
}
}
To move the view. Here are the corrosponding methods
#define PORTRAIT_KEY_OFF 216
#define LANDSCAPE_KEY_OFF 140
-(void) setViewMovedVertical:(BOOL)movedUp{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
CGRect rect = self.view.frame;
if (movedUp){
rect.origin.y -= PORTRAIT_KEY_OFF;
rect.size.height += PORTRAIT_KEY_OFF;
}
else{
rect.origin.y += PORTRAIT_KEY_OFF;
rect.size.height -= PORTRAIT_KEY_OFF;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void) setViewMovedHorizontal:(BOOL)moved{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
CGRect rect = self.view.frame;
if (moved){
rect.origin.x -= LANDSCAPE_KEY_OFF;
rect.size.width += LANDSCAPE_KEY_OFF;
}
else{
rect.origin.x += LANDSCAPE_KEY_OFF;
rect.size.width -= LANDSCAPE_KEY_OFF;
}
self.view.frame = rect;
[UIView commitAnimations];
}
And here's the method to move it back down
-(IBAction) serverIPDone: (UITextField *) sender{
if ([serverIP isFirstResponder]){
if (self.view.frame.origin.y < 0){
[self setViewMovedVertical:NO];
}
if (self.view.frame.origin.x < 0){
[self setViewMovedHorizontal:NO];
}
[serverIP resignFirstResponder];
}
}
Hope you can help! If I've anti-disambiguated (see what I did there?) the question, please let me know!
FINALLY Figured it out!!!!
Here's what was wrong: The origin did indeed change between the two landscape modes. So all you have to do is detect which version of landscape you are in, and add or subtract based on that!
-(IBAction) serverIPDone: (UITextField *) sender{
if ([serverIP isFirstResponder]){
if (self.view.frame.origin.y < 0){
[self setViewMovedVertical:NO];
}
if (self.view.frame.origin.x != 0){
[self setViewMovedHorizontal:NO];
}
[serverIP resignFirstResponder];
}
}
The previous makes sure that the keyboard is unset.
-(void) setViewMovedVertical:(BOOL)movedUp{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
CGRect rect = self.view.frame;
if (movedUp){
rect.origin.y -= PORTRAIT_KEY_OFF;
rect.size.height += PORTRAIT_KEY_OFF;
}
else{
rect.origin.y += PORTRAIT_KEY_OFF;
rect.size.height -= PORTRAIT_KEY_OFF;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void) setViewMovedHorizontal:(BOOL)moved{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
CGRect rect = self.view.frame;
if (moved){
if (leftLandscape)
rect.origin.x -= LANDSCAPE_KEY_OFF;
else
rect.origin.x += LANDSCAPE_KEY_OFF;
}
else{
if (leftLandscape)
rect.origin.x += LANDSCAPE_KEY_OFF;
else
rect.origin.x -= LANDSCAPE_KEY_OFF;
NSLog(#"after: %f",rect.origin.x);
}
self.view.frame = rect;
[UIView commitAnimations];
}
The Previous will do the actual movement of the keyboard. I got rid of the resizing, you can add it back in.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x
duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(x)) {
isPortrait = TRUE;
}
else {
isPortrait = FALSE;
leftLandscape = (x == UIInterfaceOrientationLandscapeLeft);
}
}
The Previous will make sure you set the variables isPortrait and leftLandscape correctly.
It took too long, but I'm finally DONE!!!

Simple iPad question - Safari browser - Google search bar

I'm trying to create a search bar similar to the one in safari browser on iPad. I think its a UITextview only.
On click of the control, its size will expand. And when orientation is rotated, it maintains the size accordingly.
How can I achieve that using auto-resizing option? Or do I've to code manually to achieve it?
You can do all this directly in Interface Builder.
The Search Bar component gives you the appropriate functionality. To make the bar resize properly, you just need to anchor it to the appropriate sides of the screen and make it stretchable. Try opening this file with IB for an example.
Use the below code to your controller and make sure the you have a textfield delegate.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UIInterfaceOrientation orientation = self.interfaceOrientation;
if (orientation== UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
if(textField==searchField){
CGRect searchFrame = searchField.frame;
searchFrame.size.width += 150;
searchFrame.origin.x -= 150;
[UIView beginAnimations: #"GrowTextField" context: nil];
{
searchField.frame = searchFrame;
[UIView setAnimationDuration: 0.5];
}
[UIView commitAnimations];
}
}
else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
if(textField==searchField){
CGRect searchFrame = searchField.frame;
searchFrame.size.width += 150;
searchFrame.origin.x -= 150;
[UIView beginAnimations: #"GrowTextField" context: nil];
{
searchField.frame = searchFrame;
[UIView setAnimationDuration: 0.5];
}
[UIView commitAnimations];
}
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
UIInterfaceOrientation orientation = self.interfaceOrientation;
if (orientation== UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
if(textField==searchField){
CGRect searchFrame = searchField.frame;
searchFrame.size.width -= 150;
searchFrame.origin.x += 150;
[UIView beginAnimations: #"ShrinkTextField" context: nil];
{
searchField.frame = searchFrame;
[UIView setAnimationDuration: 0.5];
}
[UIView commitAnimations];
}
}
else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
if(textField==searchField){
CGRect searchFrame = searchField.frame;
searchFrame.size.width -= 150;
searchFrame.origin.x += 150;
[UIView beginAnimations: #"ShrinkTextField" context: nil];
{
searchField.frame = searchFrame;
[UIView setAnimationDuration: 0.5];
}
[UIView commitAnimations];
}
}
}