UIImageView Zoom in and out - iphone

I want to change the images depending upon the zoom in or zoom out.
Means if i am zoom upto certain limit then it should show other image.
I added one UIImageView in UIScrollView and zoom in and out functionality is working but i am not getting how to change image during zoom in and zoom out.
And while changing image it should have fade effect in between.
Can any one help me for this?

UIScrollViewDelegate has a method - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale.
In this method you can check the scale and update the UIImageView with the appropriate UIImage.

For animating an image change, you can do:
[UIView beginAnimations:#"imageChange" context:NULL];
yourImageView.image = yourNewImage;
[UIView commitAnimations];
The change will have the fade effect you want.
About the zoom-in and zoom-out functionality I'm not sure, but you should check if the view is zooming (zooming property for your UIScrollView should be true) and then use the zoomScale property. I suppose you could do this with a timer, continuously checking if the view is zooming.

Related

iPhone How to get photo taken effect like in camera application?

I am talking about that effect when user presses button to take photo, it shrinks and moves to toolbar? How is this achieved in general?
You can create this by both scaling (applying a transform) and moving (animating the position) of the image.
I wrote about a similar animation (the Open in Background animation from Safari on iPhone) in this blog post. Not all of the code is necessary but some part of it will be useful for the animation you are trying to do.
You should
Calculate the scale factor to make the image the appropriate size
Calculate the path you want to animate the image along.
Animate a scale with the calculated scale factor
Animate a position animation along the path (using a CAKeyframeAnimation)
Since you are doing two animations at once you could benefit from using a CAAnimationGroup.
Since you are animating down to a toolbar which probably is another part of the view hierarchy than where the image is you may need to use method like
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
and
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
To translate coordinates between the different views.
so the shrink animation is achived by
[UIView beginAnimations:#"animationShrink" context:NULL];
[UIView setAnimationDuration:kSlideInAnimationDuration];
flipFlopContainer.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(shrinkAnimationFinished:)];
[UIView commitAnimations];
after this animation in shrinkAnimationFinished method you need to define a path to follow and change the position of your view accroding to that path.
See the following thread for that animation
Resize and move a UIView with Core Animation (CAKeyFrameAnimation)

clipping a UIView while zooming in

I have a custom UIView that draws some data and another more detailed view that draws some components of the first view. What I want to do is after a gesture, zoom in, hide the first view, and then show the second view to give the user the context that they are zooming in deeper.
I thought I would start by doing something like this:
CGAffineTransform zoomTransform = CGAffineTransformMakeScale(2, 2);
[UIView animateWithDuration:2 animations:^{
self.chromoMap.transform = zoomTransform;
}];
It does zoom in on that area by scaling that UIView. However, I don't want my UIView to actually grow. If my CGRect is {0, 0}, {100, 100}. I want the frame of the UIView to stay the same, but just the contents inside it to scale by 2 to give that zoom effect. How would I do that? Thanks.
It seems I can just add another transparent UIView on top of it with clip subviews enabled to get that effect.

Infinite rotating image background UIImageView

I'd like to extend a class from UIImageView,which will infinite rotate a universe image. So I can simply put this image view as background view for other UI elements.
3 questions here:
Is it the right way to extend from UIImageview class.
How to keep rotating infinite, and as background, when put it into other views, I don't need to write extra lines of code.
I wrote a rough prototype, when I put it into other views as background, all UI elements in this view are rotating with the image.
Assume that you have a UIImageView and assign an image on it and named myImgView. On viewDidLoad, add your code as bellow:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:MAXFLOAT];
myImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];
It works for me like a charm.
There is nothing wrong in extending UIImageview Class.
Create rotation animation and set repeat ON.
Do not add element on rotating image. (Add elements in base view. Insert rotating imageview at index 0).
From the UIView Class Reference:
The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
At present, UIImageView subclasses do work, especially if they don't interfere with the drawing code. Just don't complain to apple if a future iOS update breaks your subclass. It's easy enough to write your own UIView subclass to display an image. If you already have an appropriate graphics, consider just placing it in CALayer contents, instead of implementing the UIView drawRect method.
Rotations by changing UIView transform (which is of type CGAffineTransform) always take the shortest path. This means that a clockwise rotation of 270 degrees is animated as an anticlockwise rotation of 90 degrees, and any multiple of 360 degrees will do nothing. You can get 360 degree rotations if you obtain the view's layer, and animate CALayer transform (which is of type CATransform3D). See Can I use CGAffineTransformMakeRotation to rotate a view more than 360 degrees?
To make the animation repeat endlessly, the CAMediaTiming Protocol Reference recommends setting repeatCount to HUGE_VALF.

Scroll programmatically an UITableView with acceleration and deceleration

I'm trying to scroll a UITableView programmatically by tapping a button. It should be the same effect at doing it by hand or even a little bit longer in scrolling time.
First I tried to use scrollToRowAtIndexPath:atScrollPosition:animated: however it too fast and fixed in time. Also it does not have any acceleration and deceleration.
Any ideas how to scroll a UITableView programmatically as it has been scrolled by hand? I was thinking to send some custom touches to it. Do you think it could work?
Any ideas and help are greatly appreciated! Thanks!
UITableview is a subclass of UIScrollview. Like a UIScrollview, you can programmatically set UITableview's scroll position by setting the contentOffset property.
You change this property value inside an animation block so you have more control in the timing. You'll need to calculate the Y point position. Say you want to scroll to the 50th row, it might be 50 * rowHeight (assuming it's uniform height).
For example:
CGFloat calculatedPosY = 50 * rowHeight // do you own calculation of where you'd like it.
[UIView animateWithDuration:0.2
animations:^{theTableview.contentOffset = CGPointMake(0.0, calculatedPosY);}
completion:^(BOOL finished){ }];
There's another method you could use that provides more options as well
Take a look at this method:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Set your button in cell subview and use this code to scroll table programmatically on touch event of button. And if you are not setting button as a subview of table cell then modify this code as needed.
CGPoint point = [button.superview convertPoint:button.frame.origin toView:self.tableView];
CGPoint contentOffset = self.tableView.contentOffset;
contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
[self.tableView setContentOffset:contentOffset animated:YES];
Well, these are just a couple of "off the top of my head answers," but you could try using this method (in UIScrollView, of which UITableView is a subclass):
-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
Once -(CGRect) rectForRowAtIndexPath: gives you the coordinates of the cell, you can then animate to the location of cell. But rather than calling it once, call it a couple of times with a smaller offset, then with a larger and larger offsets and then a smaller ones again. I am worried that it will be difficult to make it look smooth, though.
Still, if that doesn't work, perhaps changing contentOffset value inside of a UIView animateWithDuration:delay:options:animations:completion block would work. Maybe if you use the UIViewAnimationOptionCurveEaseInOut, you can get the effect you want just calling it once. If not, run several animations each part of way and covering great and greater distances before slowing down.

Image is getting blury when Zooming In

I am trying to zoom to a image in my view. For that I used UIScrollView and in that scrollView I placed a ImageView. I had set min and max zoom scale of scrollview.
I used this code too..
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imgView;
}
The problem is My image gets blury while zoomig. Any one have idea how to redraw image with the zoom size.
hi set maximumZoomScale=1.0f. it wont blurr :)