in iOS5 my methods for switching pictures in gallery are bad - iphone

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) { ... }

Related

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;
}
}

How can I make this snippet prettier/better/less lines?

I've been trying to minimize all my functions in my app but can't really find how to make this function better, maybe someone is a lot better at this than me? :)
-(void)showRivBoxWithAnimtation:(BOOL)yesno {
if(yesno) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
if ([self alpha] > 0) {
[self setAlpha:0.0];
[appDelegate.JSONparser setDelegate:self.delegate]; //Give back the JSONparser to the parent!
} else {
[self setAlpha:1.0];
}
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(clearRivBoxContent:finished:context:)];
[UIView commitAnimations];
} else {
if ([self alpha] > 0) {
[self setAlpha:0.0];
[appDelegate.JSONparser setDelegate:self.delegate]; //Give back the JSONparser to the parent!
} else {
[self setAlpha:1.0];
}
}
}
This is what I would do:
-(void)showRivBoxWithAnimtation:(BOOL)yesno {
[UIView animateWithDuration:yesno ? 0.2 : 0.0
animations:^{
if ([self alpha] > 0) {
[self setAlpha:0.0];
[appDelegate.JSONparser setDelegate:self.delegate]; //Give back the JSONparser to the parent!
} else {
[self setAlpha:1.0];
}
}
completion:^(BOOL finished){
if (finished) {
// Do the stuff from clearRivBoxContent:finished:context:
}
}];
}
Unless the order of operations wrt. alpha is critical; this is probably the smallest it can be made.
-(void)showRivBoxWithAnimtation:(BOOL)yesno {
if ([self alpha] > 0) {
[self setAlpha:0.0];
[appDelegate.JSONparser setDelegate:self.delegate]; //Give back the JSONparser to the parent!
} else {
[self setAlpha:1.0];
}
if(yesno) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(clearRivBoxContent:finished:context:)];
[UIView commitAnimations];
}
}

check if pinch is released in UIPinchGestureRecognizer

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];
}

How can we implement pagecurl animation in tableview?

Ha ii every body ,how can we implement a page-curl in tableview,i have a tableview which contains pages of the book and i have implement the touch events in the tableview cell for next chapter and previous chapter,left-swipe for next and right-swipe for previous chapter,when we swipe the tableview it reloads the next chapter and previous chapter content,it really works well but i want it in a page-curl animation,when the user swipe left or right tableview loads content with a page-curl animation.Is that possible to do in a tableview cell?my code for left and right swipe for chapter navigation as follows.
-(void) handleSwipeGesture:(UISwipeGestureRecognizer*)recognizer {
if(![delegate.selectedChapter isEqualToString:[NSString stringWithFormat:#"%d",[DbHandler mNumberOfChaptersInBook:delegate.selectedBook]]]) {
// if the currentChapter is the last then do nothing
delegate.selectedChapter = [NSString stringWithFormat:#"%d",[delegate.selectedChapter intValue] + 1];
[delegate reloadVerses];
[self resetReadViewToVerse:1];
[table removeGestureRecognizer:recognizer];
}
if (recognizer.state==UIGestureRecognizerStateBegan ) {
self.table.scrollEnabled = NO;
}
else if(recognizer.state==UIGestureRecognizerStateEnded) {
self.table.scrollEnabled = YES;
}
return;
}
-(void) handleSwipeGestureleft:(UISwipeGestureRecognizer*)recognizer {
if(![delegate.selectedChapter isEqualToString:#"1"])
{
delegate.selectedChapter = [NSString stringWithFormat:#"%d",[delegate.selectedChapter intValue] - 1];
[delegate reloadVerses];
[self resetReadViewToVerse:1];
[table removeGestureRecognizer:recognizer];
}
if (recognizer.state==UIGestureRecognizerStateBegan ) {
self.table.scrollEnabled = NO;
}
else if(recognizer.state==UIGestureRecognizerStateEnded) {
self.table.scrollEnabled = YES;
}
return;
}
Thanks in advance.
I got the answer.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelay:0.0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:#selector(animCompleteHandler:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView commitAnimations];
just put this code inside the methods.Thanks

iphone : Switching views like a picture book, problem with removeFromSuperView

Right I know how to do the pictue book effect but I struggling to remove the view
So page 1, I add the second page view via addSubview (via a swipe). I also increment a counter so I know what page I am on.
So how do I return to page 1? See I thought it would be self.view removeFromSuperview but that crashes when you try to go back to page 2
Code below
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
//NSLog(#"swipe right to left");
UniversalAppAppDelegate *appDelegate = (UniversalAppAppDelegate *)[[UIApplication sharedApplication] delegate];
PictureBookViewController *viewController2 =(PictureBookViewController *) [appDelegate getViewControllerForViewID:#"81"];
[UIView beginAnimations:Nil context:nil];
[UIView setAnimationDuration:0.6];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];
self.viewController = viewController2;
self.viewController.pageNo = self.pageNo + 1;
[self.view addSubview:self.viewController.view];
[UIView commitAnimations];
[viewController2 release];
} else {
//NSLog(#"swipe left to right");
NSLog([NSString stringWithFormat:#"%d", self.pageNo]);
if (self.pageNo > 0) {
[UIView beginAnimations:Nil context:nil];
[UIView setAnimationDuration:0.6];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];
//self.viewController = viewController2;
//self.viewController.pageNo = self.pageNo - 1;
//[self.view addSubview:self.viewController.view];
[self.view removeFromSuperview];
//[self.view addSubview:self.viewController.view];
[UIView commitAnimations];
}
}
}
Update:
Each view has its own view controllers.
You want to remove your viewController's view, not the container's view.
[self.viewController.view removeFromSuperview];
This seems to work and not crash? I am now convinced it right. I just seem to need a way of removing the last added subview. My first view is not the superview
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
//NSLog(#"swipe right to left");
UniversalAppAppDelegate *appDelegate = (UniversalAppAppDelegate *)[[UIApplication sharedApplication] delegate];
PictureBookViewController *viewController2 =(PictureBookViewController *) [appDelegate getViewControllerForViewID:#"82"];
[UIView beginAnimations:Nil context:nil];
[UIView setAnimationDuration:0.6];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];
self.viewController = viewController2;
self.viewController.pageNo = self.pageNo + 1;
self.viewController.lastViewController = self;
[self.view addSubview:self.viewController.view];
[UIView commitAnimations];
[viewController2 release];
} else {
//NSLog(#"swipe left to right");
NSLog([NSString stringWithFormat:#"%d", self.pageNo]);
if (self.pageNo > 0) {
[UIView beginAnimations:Nil context:nil];
[UIView setAnimationDuration:0.6];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];
self.pageNo --;
self.view = self.viewController.view;
[self.view removeFromSuperview ];
self.view = self.lastViewController.view;
[UIView commitAnimations];
}
}
}