UIView Animation for ImageView in ios - ios

i Have requirement were i have to animate image view from current position to full screen in grid view. i made images into Grid view.
- (void)viewDidLoad
{
[super viewDidLoad];
imagesArray = [[NSMutableArray alloc] init];
for (int i=1; i<11; i++) {
[imagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"image%d.png", i]]];
NSLog(#"imagesArray:%#", imagesArray);
}
float horizontal = 20.0;
float vertical = 20.0;
for(int i=0; i<[imagesArray count]; i++)
{
if((i%3) == 0 && i!=0)
{
horizontal = 20.0;
vertical = vertical + 220.00 + 20.0;
}
imagesView = [[UIImageView alloc] initWithFrame:CGRectMake(horizontal, vertical, 310.0, 220.00)];
imagesView.userInteractionEnabled = YES;
[imagesView setImage:[imagesArray objectAtIndex:i]];
imagesView.tag = i;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[imagesView addGestureRecognizer:tap];
[self.view addSubview:imagesView];
horizontal = horizontal + 310.0 + 20.0;
}
}
here i have created grid view with images, like i have 10 images. i made 3x3 grid view.
next i have add tap gesture to it and i am getting the image view position by calling with tag
-(void)handleTap:(UITapGestureRecognizer *)recognizer
{
UIView *piece = recognizer.view;
[UIView animateWithDuration:1.0 animations:^{
piece.frame = CGRectMake(piece.frame.origin.x, piece.frame.origin.y, 1000, 700);
} completion:^(BOOL finished) {
}];
}
Can you say me how to make image view animate from current position to full screen by tapping on it.

Since you're adding the views to self.view of your view controller, you can assume that self.view is occupying the entire screen of the device. Therefore, you can animate to its bounds:
[UIView animateWithDuration:1.0 animations:^{
piece.frame = self.view.bounds;
} completion:nil];
Note that if piece must have a parent view that spans across the entire screen so that your inner view won't be clipped during animation.

This code will help u to add the animation effect like zoom your imageview that your are expecting. You need to customize once the animation is over show the full screen image view.
-(void)addZoomAnimationToZoomViewController:(UIImageView *)zoomView
{
CABasicAnimation *zoomAnimation = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
[zoomAnimation setDuration:0.8];
[zoomAnimation setRepeatCount:1];
[zoomAnimation setFromValue: [NSNumber numberWithFloat:0.1]];
[zoomAnimation setToValue: [NSNumber numberWithFloat:5.0] ];
[zoomAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[zoomView layer] addAnimation:zoomAnimation forKey:#"zoom"];
}

Related

iOS Layer animation with auto layout

I have a simple container view(green) and two sub views(red and blue) as below.
The container view is not applying auto layout and I config its size & location by frame.
While the sub views are applying auto layout(see code below)
#implementation XXView {
UIView *_leftView;
UIView *_rightView;
}
- (instancetype)init {
self = [super initWithFrame:CGRectZero];
if (self) {
[self setupViewHierarchy];
[self setupConstraints];
}
return self;
}
- (void)setupViewHierarchy {
_leftView = [[UIView alloc] init];
_leftView.backgroundColor = UIColor.redColor;
_leftView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_leftView];
_rightView = [[UIView alloc] init];
_rightView.backgroundColor = UIColor.blueColor;
_rightView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_rightView];
self.backgroundColor = UIColor.greenColor;
}
- (void)setupConstraints {
[NSLayoutConstraint activateConstraints:#[
[_leftView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:10],
[_leftView.topAnchor constraintEqualToAnchor:self.topAnchor],
[_leftView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
[_leftView.widthAnchor constraintEqualToConstant:50],
[_rightView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-10],
[_rightView.topAnchor constraintEqualToAnchor:_leftView.topAnchor],
[_rightView.bottomAnchor constraintEqualToAnchor:_leftView.bottomAnchor],
[_rightView.widthAnchor constraintEqualToAnchor:_leftView.widthAnchor],
]];
}
...
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
XXView *tv = [[XXView alloc] init];
CGFloat width = self.view.bounds.size.width;
tv.frame = CGRectMake(50, 400, width-100, 100);
[self.view addSubview:tv];
self.tv = tv;
}
Then I would like to animate the container's width change by using the CABasicAnimation as below:
- (void)startAnimation {
[CATransaction begin];
CGFloat width = self.bounds.size.width;
CABasicAnimation *widthAnimation = [CABasicAnimation animationWithKeyPath:#"bounds.size.width"];
widthAnimation.fromValue = #(width/2);
widthAnimation.toValue = #(width);
widthAnimation.duration = 1;
[self.layer addAnimation:widthAnimation forKey:#"123"];
[CATransaction commit];
}
However, the animation is not as I would expect. I would expect the left view moves as the container's leading side and the right view does as the trailing side.
What I see is, the green view expands as expected and the left view moves as green view's leading side. However, the right view is always keeping the same distance to left view. Below is the screenshot taken at the beginning of the animation.
Why the animation is not working as expected?
The problem is that your CABasicAnimation is modifying the bounds of the layer ... but as far as auto-layout is concerned that does not change the width of the view.
It's not really clear what your goal is here, but if you change your animation method to this it might get you on your way:
- (void)startAnimation {
CGRect f = self.frame;
CGFloat fw = f.size.width;
f.size.width = fw / 2;
self.frame = f;
[self layoutIfNeeded];
f.size.width = fw;
[UIView animateWithDuration:1.0 animations:^{
self.frame = f;
[self layoutIfNeeded];
}];
}

Resize uiscrollview after scaling so as to enable paging

I am having a scrollview containing 2 image views in one page only.I am scaling scrollview so as to cover full screen on tapping.Now i want after scaling, paging should be enabled.But then it skip every alternate image view. Any help would be appreciated.`
kNumViews = 10;
vScrollObjectHeight = 230; // half screen
vScrollObjectWidth = 160; // half screen
{
for (i = 1; i <= kNumViews; i++)
{
NSString *imageName = [NSString stringWithFormat:#"imageV%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView sizeToFit];
imageView.layer.borderColor = [UIColor grayColor].CGColor;
imageView.layer.borderWidth = 1.5f;
CGRect rect = imageView.frame;
rect.size.height = vScrollObjHeight;
rect.size.width = vScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[scrollView2 addSubview:imageView];
}
[self layoutScrollViews];
self.swipeRecognizerUp = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scaleMove:)];
[self.swipeRecognizerUp setDelegate:self];
[scrollView2 addGestureRecognizer:self.swipeRecognizerUp];
self.swipeRecognizerDown = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scaleDown:)];
[self.swipeRecognizerDown setDelegate:self];
[scrollView2 addGestureRecognizer:self.swipeRecognizerDown];
self.swipeRecognizerDown.enabled = NO;
}
-(void)scaleMove:(id)sender {
[[[(UIPanGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[sender view]];
CGPoint velocity = [sender velocityInView:self.view];
if(abs(velocity.y) - abs(velocity.x) > 500 && velocity.y < 0){
[sender view].transform = CGAffineTransformMakeScale(1.0, 1.0);
//[sender view].transform = CGAffineTransformMakeTranslation(0, 230);
[UIView animateWithDuration:0.5
animations:^(){
CGAffineTransform scale = CGAffineTransformMakeScale(2.0, 2.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0,-230);
[sender view].transform = CGAffineTransformConcat(translate, scale);
//NSLog(#"%f %f",[sender view].frame.size.width,[sender view].frame.size.height);
scrollView2.pagingEnabled = YES;
}
completion:nil];
self.swipeRecognizerDown.enabled = YES;
self.swipeRecognizerUp.enabled = NO;
}
}
}
`
found answer of it...I have set the width of scrollview half and then subclasses the scrollview so as to override pointInside and then normally scaled scrollview.found answer of it...I have set the width of scrollview half and then subclasses the scrollview so as to override pointInside and then normally scaled scrollview.

How to add multiple UIImageViews to paging UIScrollView?

I have a UIScrollView with paging enabled with multiple subviews on the page: a UIButton, UIwebview and UIImageView. Both the webview and the image change on every page. This works fine. I used Apples scrolling image paging example to get me started.
But when I add a second UIImageView, the position of image I have in place already gets the new values and the new image doesn't display.
This is the code inside viewdidload for the first image (works fine):
// load all the images from our bundle and add them to the scroll view
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIImage *image2 = [UIImage imageNamed:imageName];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView2.frame;
rect.size.height = imageviewScrollObjHeight;
rect.size.width = imageviewScrollObjWidth;
// Get the Layer of any view
CALayer * imageLayer = [imageView2 layer];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:7.0];
// You can even add a border
[imageLayer setBorderWidth:1.0];
[imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]];
imageView2.frame = rect;
imageView2.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView2];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
This is the code to layout the first image on every page, different image per page(outside viewdidload)(works fine):
// layout images for imageview1
- (void)layoutScrollImages
{
UIImageView *imageView = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 10;
for (imageView in subviews)
{
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag > 0)
{
CGRect frame = imageView.frame;
frame.origin = CGPointMake(curXLoc, 50);
imageView.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
This is the code for the second image (inside viewdidload): (when i remove [self layoutNavScrollImages]; the image loads only on the first page)
for (i = 1; i <= kNumImages; i++)
{
UIImage *navBarImage = [UIImage imageNamed:#"navigationbar.png"];
UIImageView *imageViewNavBar = [[UIImageView alloc] initWithImage:navBarImage];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect navBarRect = imageViewNavBar.frame;
navBarRect.size.height = 44;
navBarRect.size.width = 320;
navBarRect.origin.x = 0;
navBarRect.origin.y = 0;
/* Get the Layer of any view
CALayer * imageLayer = [imageView3 layer];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:7.0];
// You can even add a border
[imageLayer setBorderWidth:1.0];
[imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]];
*/
imageViewNavBar.frame = navBarRect;
imageViewNavBar.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageViewNavBar];
}
[self layoutNavScrollImages];
And the code outside viewdidload:(this overwrites the position of the first image)
- (void)layoutNavScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
The way to do this is to make one (big) UIView and add every UIImageView as a subview of that UIView. And then add the UIView as a subview to UIScrollView.
[totalView addSubview:imageView1];
[totalView addSubview:imageView2;
[totalView addSubview:buttonView1];
[totalView addSubview:buttonView2];
[totalView addSubview:webView];
[scrollView addSubview:totalView];
Be sure to set the right content size or scrollview wont work:
[scrollView setContentSize:CGSizeMake((320*kNumImages), 411)];
Setup views and tag the UIView (inside viewdidload):
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
//... code to setup views
totalView.tag = i;
}
[self layoutViews]; // now place the views in serial layout within the scrollview
Then to layout the view on every page:
- (void)layoutViews
{
UIView *view = nil;
NSArray *subviews = [scrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
}
I hope this is as clear as possible. If anyone thinks this is not the right way to do it please comment.

Make UIImageView animate after another UIImageView has finished

I am trying to animate the dealing of cards. Currently I have two cards, the dealer card (dCard1) and the player card (pCard1).
The player card should be dealt and as soon as it is dealt, the dealer card should then be dealt.
What is currently happening is the player card will animate properly when I press the deal button, but instead of waiting till the player card is done animating, the dealer card just appears at the dealerTo location. It does not animate. I am new to doing animations any help would be greatly appreciated.
-(void) animateHandsDealt: (Hand*) pHand:(Hand*) dHand{
pCard1= [[UIImageView alloc] initWithFrame:CGRectMake(125, 0, 75, 110)];
dCard1= [[UIImageView alloc] initWithFrame:CGRectMake(125, 0, 75, 110)];
CGFloat start = 0;
CGFloat duration = 1;
CGPoint playerTo = CGPointMake(120, 300);
CGPoint dealerTo = CGPointMake(120, 70);
pCard1.image = [UIImage imageNamed:[[pHand.cardArr objectAtIndex:0 ] imagePath]];
[self.view addSubview: pCard1];
[playerImages addObject:pCard1];
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:#"position" ];
move.fillMode = kCAFillModeBoth;
move.beginTime = start;
[move setToValue:[NSValue valueWithCGPoint:playerTo]];
[move setDuration:duration];
move.removedOnCompletion = FALSE;
[[pCard1 layer] addAnimation:move forKey:#"position"];
dCard1.image = [UIImage imageNamed:#"back-red-75-1.png"];
[self.view addSubview: dCard1];
CABasicAnimation *move2 = [CABasicAnimation animationWithKeyPath:#"position" ];
[move2 setToValue:[NSValue valueWithCGPoint:dealerTo]];
move2.beginTime = start + duration;
[move2 setDuration:duration];
move2.fillMode = kCAFillModeBoth;
move2.removedOnCompletion = FALSE;
[[dCard1 layer] addAnimation:move2 forKey:#"position"];
}
If you're just animating the frame of the view, you can use the much simpler UIView animateWithDuration... methods.
You chain them by using the animateWithDuration:animations:completion: version. You can specify the second animation in the completion: block of the first animation.
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW109
I don't have access to your classes (of course), but I think this is pretty close to "drop-in-ready" code. What do you think?
- (void)deal {
// Of course, you'd replace these NSString objects with actual card objects...
NSArray *array = [NSArray arrayWithObjects:#"pCard1", #"dCard1", #"pCard2", #"dCard2", #"pCard3", #"dCard3", nil];
[self performSelectorOnMainThread:#selector(animateDealWithArray:) withObject:array waitUntilDone:YES];
}
- (void)animateDealWithArray:(NSArray *)array {
// constants that won't change call to call
static CGFloat duration = 1;
static CGSize cardSize = {75, 110};
static CGPoint playerTo = {120, 300};
static CGPoint dealerTo = {120, 70};
// Seems like you want the card to start at the top of the "deck" whether it's a player card or not
UIImageView *card = [[UIImageView alloc] initWithFrame:CGRectMake(125, 0, 75, 110)];
// Figure out if the first object (the one we're currently using) is a player or dealer card
if ([[array objectAtIndex:0] isKindOfClass:[PlayerCard Class]]) {
// Player card, so show the "face"
[card setImage:[UIImage imageNamed:[(PlayerCard *)[array objectAtIndex:0] imagePath]]];
[UIView animateWithDuration:duration
animations:^{
[card setFrame:CGRectMake(playerTo.x, playerTo.y, cardSize.width, cardSize.height)];
}
completion:^(BOOL finished) {
[self dealNext:array];
}
];
} else {
// Dealer card, so show the back
[card setImage:[UIImage imageNamed:#"back-red-75-1.png"]];
[UIView animateWithDuration:duration
animations:^{
[card setFrame:CGRectMake(dealerTo.x, dealerTo.y, cardSize.width, cardSize.height)];
}
completion:^(BOOL finished) {
[self dealNext:array];
}
];
}
}
- (void)dealNext:(NSArray *)array {
int count = [array count];
if (count > 1) {
NSArray *newArray = [array subarrayWithRange:NSMakeRange(1, count - 1)];
[self animateDealWithArray:newArray];
}
}

Animate UIView height from bottom to top

I'm doing a simple animation of UIView height so that it reveals.
By default it seems to be revealing from top to bottom, and I want it to reveal bottom to top.
I have the UIView anchored to the bottom of the screen.
I'm sure it something simple i'm missing..... any tips?
Thanks
I really think the simplest way to accomplish this would be to animate BOTH the height and the y properties of the view. If they happen along the same curve, it should look completely seamless to the user. As you are animating the height to 0, also animate the y component to the original y + the original height.
UIView *view = ...;
float originalY = view.frame.origin.y;
float originalH = view.bounds.size.height;
[UIView animateWithDuration:1.2f delay:1.0f options:UIViewAnimationCurveEaseInOut animations:^{
view.frame = CGRectMake(view.frame.origin.x, (originalY + originalH), view.bounds.size.width, 0);
}completion:^(BOOL finished) {
NSLog(#"Animation is complete");
}];
I believe this would give the look and feel of a collapsing view. I haven't tried this out in code, but I see no reason why it wouldn't be possible like this.
hide under bottom
[self animateViewHeight:myView withAnimationType:kCATransitionFromBottom];
for reverse animation
[self animateViewHeight:myView withAnimationType:kCATransitionFromTop];
...
- (void)animateViewHeight:(UIView*)animateView withAnimationType:(NSString*)animType {
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionPush];
[animation setSubtype:animType];
[animation setDuration:0.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[animateView layer] addAnimation:animation forKey:kCATransition];
animateView.hidden = !animateView.hidden;
}
Like a dog with a bone I figured this out....
Instead of animating the frame height, I applied a transform to the view and set the anchor point of the layer.
//set the anchor point to the bottom of the view
[self setAnchorPoint:CGPointMake(0.5, 1.0) forView:hostView];
//Scale the height to close to zero
hostView.transform = CGAffineTransformMakeScale(1, 0.00001);
If I put 0 as the y scale, the view behaves weird.... at the end of the animation i just set it to hidden.
On the way back up I just use the Identity Transform (reset it)
hostView.transform = CGAffineTransformIdentity;
Note that changing my anchor point shifted the position of my view. See this post for the setAnchorPoint method which normalises the view after setting the anchorPoint
Changing my CALayer's anchorPoint moves the view
Instead you could try putting it in a view with clipsToBounds = YES and then animate it from the bottom to the middle of the view, like so:
viewToAnimate.frame = CGRectMake(viewToAnimate.frame.origin.x,
viewToAnimate.superview.frame.size.height,
viewToAnimate.frame.size.width,
viewToAnimate.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{
viewToAnimate.center = viewToAnimate.superview.center;
}];
This way, you don't have to set the height to 0, and it solves any problems with autoresizing within the view.
As requested, this is the code that I'm using... I'm using a CAKeyFrameAnimation, which may be a bit more than what you're looking for. It would probably work the same with a CABasicAnimation, I'm just showing you this code because I already have it written.
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
springLayer = [[CALayer alloc] init];
springLayer.backgroundColor = [UIColor redColor].CGColor;
springLayer.anchorPoint = CGPointMake(0, 1);
springLayer.frame = CGRectMake(125, 285, 100, 115);
[springLayer setNeedsDisplay];
[self.layer addSublayer:springLayer];
[self test];
}
return self;
}
-(void)test {
CAKeyframeAnimation *heightAnim = [[CAKeyframeAnimation alloc] init];
heightAnim.duration = 3;
heightAnim.removedOnCompletion = NO;
heightAnim.fillMode = kCAFillModeForwards;
heightAnim.beginTime = CACurrentMediaTime() + 0.25;
NSMutableArray *v = [[NSMutableArray alloc] init];
NSMutableArray *t = [[NSMutableArray alloc] init];
float dest = 250;
float difference = 135;
while (difference > 1.0) {
[v addObject:[NSNumber numberWithFloat:dest-difference]];
[t addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
difference *= 0.7;
[v addObject:[NSNumber numberWithFloat:dest+difference]];
[t addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
difference *= 0.7;
}
heightAnim.values = v;
heightAnim.timingFunctions = t;
[springLayer addAnimation:heightAnim forKey:#"bounds.size.height"];
}
one way I've done it with an AdWhirlView, hide it below the screen, then animate it up;
AdWhirlView *adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
adWhirlView.delegate = self;
adWhirlView.frame = CGRectMake(0, 430+kAdWhirlViewHeight, kAdWhirlViewWidth, kAdWhirlViewHeight);
[self.parentViewController.view insertSubview:adWhirlView belowSubview:self.view];
[UIView beginAnimations:#"AdWhirlIn" context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
adWhirlView.frame = CGRectMake(0, 430, kAdWhirlViewWidth, kAdWhirlViewHeight);
[UIView commitAnimations];

Resources