Objective-C - Animate button with blocks? - ios

I have a UIScrollView and a UIButton. I am using blocks to animate them however the button won't animate while the UIScrollView does? Can you not animate a button like this?
Any help would be appreciated greatly!
- (void)viewDidLoad
{
[super viewDidLoad];
draw1 = 0;
scrollView.frame = CGRectMake(0, -200, 768, 200);
[scrollView setContentSize:CGSizeMake(768, 200)];
openMenu.frame = CGRectMake(680, 100, 55, 55);
}
- (IBAction)openMenu:(id)sender {
if (draw1 == 0) {
draw1 = 1;
CGRect optionsDrawer = scrollView.frame;
CGRect optionsButton = openMenu.frame;
optionsDrawer.origin.y += 200;
[UIView animateWithDuration:0.5
animations:^{
scrollView.frame = optionsDrawer;
openMenu.frame = optionsButton;
}];
} else {
draw1 = 0;
CGRect optionsDrawer = scrollView.frame;
CGRect optionsButton = openMenu.frame;
optionsDrawer.origin.y -= 200;
[UIView animateWithDuration:0.5
animations:^{
scrollView.frame = optionsDrawer;
openMenu.frame = optionsButton;
}];
}
}

Ah! I forgot to add the origin to the button as well! Oops!
Fixed with adding to each:
optionsButton.origin.y += 200;

Related

I want to animate UITableView on Button Click

I have UITableView and i want to animate when user click on Button.
For Selected State --> BOTTOM TO TOP
For UnSelected State --> TOP TO BOTTOM
For that I have tried Below Code.
My TableView frame (0,39, width, height)
- (IBAction)onInviteFriendsButtonClick:(UIButton *)sender {
sender.selected =! sender.selected;
if (sender.selected) {
CGRect napkinBottomFrame = self.tblVWInviteFriend.frame;
napkinBottomFrame.origin.y = [UIScreen mainScreen].bounds.size.height;
self.tblVWInviteFriend.frame = napkinBottomFrame;
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlUp animations:^{
self.tblVWInviteFriend.frame = CGRectMake(0, 39, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
} completion:^(BOOL finished){/*done*/}];
}
else {
CGRect napkinBottomFrame = self.tblVWInviteFriend.frame;
napkinBottomFrame.origin.y = 39;
self.tblVWInviteFriend.frame = napkinBottomFrame;
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlDown animations:^{
self.tblVWInviteFriend.frame = CGRectMake(0, -[UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
} completion:^(BOOL finished){/*done*/}];
}
}
When i click button it will animate from BOTTOM TO TOP properly.
but when i click button again it will again gone from BOTTOM TO TOP.
Because you have set frame.origin.y of tableview is -y or XY coordinate.
self.tblVWInviteFriend.frame = CGRectMake(0, -[UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
replace with this
self.tblVWInviteFriend.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
Try this :
- (IBAction)onInviteFriendsButtonClick:(UIButton *)sender {
sender.selected =! sender.selected;
if (sender.selected) {
CGRect napkinBottomFrame = txtAOP.frame;
napkinBottomFrame.origin.y = [UIScreen mainScreen].bounds.size.height;
txtAOP.frame = napkinBottomFrame;
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlUp animations:^{
txtAOP.frame = CGRectMake(0, 39, txtAOP.frame.size.width, txtAOP.frame.size.height);
} completion:^(BOOL finished){/*done*/}];
}
else {
CGRect napkinBottomFrame = txtAOP.frame;
napkinBottomFrame.origin.y = -[UIScreen mainScreen].bounds.size.height;
txtAOP.frame = napkinBottomFrame;
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlDown animations:^{
txtAOP.frame = CGRectMake(0,39 , txtAOP.frame.size.width, txtAOP.frame.size.height);
} completion:^(BOOL finished){/*done*/}];
}
}

UIView jumps down at beginning

I need to change UIView position on scroll of tableview. When i change the frame size of UIView in the animation block it goes down and than move to correct position. Please look at my code.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint scrollVelocity = [_collectionViewLeaderboard.panGestureRecognizer velocityInView:_collectionViewLeaderboard.superview];
if (scrollVelocity.y > 0.0f){
NSLog(#"going down");
[UIView animateWithDuration:0.3f
animations:^ {
_headerview.frame = CGRectMake(0, 0, _headerview.frame.size.width, _headerview.frame.size.height);
_headerviewSecond.frame = CGRectMake(0, _headerview.frame.size.height, _headerviewSecond.frame.size.width, _headerviewSecond.frame.size.height);
self.collectionViewLeaderboard.frame = CGRectMake(self.view.frame.origin.x, _headerviewSecond.frame.size.height+_headerview.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
frameconditon = _headerview.frame;
} completion:^ (BOOL completed) {
}];
}
else if (scrollVelocity.y < 0.0f){
NSLog(#"going up");
//CGAffineTransform transform = CGAffineTransformMake(1, 0, 0, 1, _headerview.frame.origin.x, _headerview.frame.origin.y);
[UIView animateWithDuration:5.0 animations:^{
NSLog(#"test");
_headerview.frame = CGRectMake(0, -(_headerview.frame.size.height), _headerview.frame.size.width, _headerview.frame.size.height);
_headerviewSecond.frame = CGRectMake(0, (_headerview.frame.size.height)-40, _headerviewSecond.frame.size.width, _headerviewSecond.frame.size.height);
} completion:^(BOOL finished) {
}];
self.collectionViewLeaderboard.frame = CGRectMake(self.view.frame.origin.x, _headerviewSecond.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
}
}
headerview should move to top of view and it work but when i run it first time, it goes down and back to correct postion. How will i change constraints at the time of scrolling.
For constraint based views try to animate through changing constraints constant property using an outlet.

Cropping image same as updated version of Instagram

I want to crop image in my application same as updated Instagram.
I have create one demo for it.
But i got 1 issue in this demo.
Issue is that, when i zoomIn image and then try to zoom out little bit that time UIImageview zooming out full. Please check demo u got my point which i try to explain
This is my demo link
https://drive.google.com/file/d/0Bw367uJS6w22OTNwTmdCNW5Ta3M/view?usp=sharing
you can this library if you want instagram like camera and imagepicker
https://github.com/ytakzk/Fusuma
In VPImageCropperViewController file You will find a method
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer you have to change in that code to work zoom out functionality.
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
UIView *view = self.showImgView;
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
//pinchGestureRecognizer.scale = 1;
self.showImgView.contentMode=UIViewContentModeScaleAspectFit;
NSLog(#"%f",pinchGestureRecognizer.scale);
// pinchGestureRecognizer.view.transform = CGAffineTransformScale(pinchGestureRecognizer.view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
// pinchGestureRecognizer.scale = 1;
}
else if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"original image size%f",self.originalImage.size.width);
NSLog(#"latestFrame image size%f",self.latestFrame.size.width);
// NSLog(#"original image size%f",self.originalImage.size.width);
// NSLog(#"original image size%f",self.originalImage.size.width);
/* if(pinchGestureRecognizer.scale <= 1.0)
{
// small
CGRect newFrame = self.showImgView.frame;
UIImage *img=[self imageWithImage:self.originalImage scaledToSize:self.cropFrame.size];
newFrame = [self handleScaleOverflow:CGRectMake(0, 0, img.size.width, img.size.height)];
newFrame = [self handleBorderOverflow:CGRectMake(0, 0, img.size.width, img.size.height)];
newFrame = [self handleScaleOverflow:newFrame];
newFrame = [self handleBorderOverflow:newFrame];
[UIView animateWithDuration:BOUNDCE_DURATION animations:^{
self.showImgView.frame = newFrame;
self.latestFrame = newFrame;
self.showImgView.contentMode=UIViewContentModeScaleAspectFit;
}];
}*/
/* if(pinchGestureRecognizer.scale > 1.0)
{*/
// big
CGRect newFrame = self.showImgView.frame;
newFrame = [self handleScaleOverflow:newFrame];
newFrame = [self handleBorderOverflow:newFrame];
[UIView animateWithDuration:BOUNDCE_DURATION animations:^{
self.showImgView.frame = newFrame;
self.latestFrame = newFrame;
}];
//}
}
}
May be it will help you.
Do changes in VPImageCropperViewController.m file
// pinch gesture handler
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
UIView *view = self.showImgView;
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
self.showImgView.contentMode=UIViewContentModeRedraw;
self.editedImage=self.showImgView.image;
}
else if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded) {
if(view.frame.size.width <= self.cropFrame.size.width && view.frame.size.height <= self.cropFrame.size.height)
{
// Zoom out
CGRect newFrame = self.showImgView.frame;
UIImage *img=[self imageWithImage:self.originalImage scaledToSize:self.cropFrame.size];
newFrame = [self handleScaleOverflow:CGRectMake(0, 0, img.size.width, img.size.height)];
newFrame = [self handleBorderOverflow:CGRectMake(0, 0, img.size.width, img.size.height)];
newFrame = [self handleScaleOverflow:newFrame];
newFrame = [self handleBorderOverflow:newFrame];
[UIView animateWithDuration:BOUNDCE_DURATION animations:^{
self.showImgView.frame = newFrame;
self.latestFrame = newFrame;
self.showImgView.contentMode=UIViewContentModeScaleAspectFit;
CGRect cropRect = AVMakeRectWithAspectRatioInsideRect(self.showImgView.image.size, self.showImgView.frame);
NSLog(#"frame %f %f ",cropRect.size.width,cropRect.size.height);
}];
}
else
{
// Zoom In
CGRect newFrame = self.showImgView.frame;
newFrame = [self handleScaleOverflow:newFrame];
newFrame = [self handleBorderOverflow:newFrame];
[UIView animateWithDuration:BOUNDCE_DURATION animations:^{
self.showImgView.frame = newFrame;
self.latestFrame = newFrame;
}];
}
}
}

Frame animation doesnt work

viewDidLoad :
topBarMenu = [[TopBarMenu alloc] initWithFrame:CGRectMake(0, 64, 1024, 0)];
[self.view addSubview:topBarMenu];
topBarMenu.clipsToBounds = YES;
- (void)menuButton_TouchUpInside:(TopBarIcon *)sender
{
isTopBarMenuShown = !isTopBarMenuShown;
if (isTopBarMenuShown) {
[UIView animateWithDuration:1.5 animations:^{
topBarMenu.frame = CGRectMake(0, 64, 1024, 600);
}];
}else {
[UIView animateWithDuration:1.5 animations:^{
topBarMenu.frame = CGRectMake(0, 64, 1024, 0);
}];
}
}
In my code i want to animate showing and hiding my menu. Showing is very stepped and dosen't look nice. Hiding immediately remove the screen without any animation. How to solve this problem ?
You need to set constant when you are using Autolayout and Animation. So, set constant before you begin the Animation. So, create outlet of constrain of y position.
topbaryposition.constant=0; (IBOutlet of Top position (Y postion of Topbar))
- (void)menuButton_TouchUpInside:(TopBarIcon *)sender
{
isTopBarMenuShown = !isTopBarMenuShown;
if (isTopBarMenuShown) {
[UIView animateWithDuration:1.5 animations:^{
topBarMenu.frame = CGRectMake(0, 64, 1024, 600);
}];
}else {
[UIView animateWithDuration:1.5 animations:^{
topBarMenu.frame = CGRectMake(0, 64, 1024, 0);
}];
}
}
Something like ,
Edit :-
I found your Issue .
Your issue is seeting the frame in else when you hiding the topbar.you have set only height as 0 but you also need to set y positioan as 0..
Something like ,
topBarMenu.frame = CGRectMake(0, 0, 1024, 0);
- (void)menuButton_TouchUpInside:(TopBarIcon *)sender
{
isTopBarMenuShown = !isTopBarMenuShown;
if (isTopBarMenuShown) {
[UIView animateWithDuration:1.5 animations:^{
topBarMenu.frame = CGRectMake(0, 64, 1024, 600);
}];
}else {
[UIView animateWithDuration:1.5 animations:^{
topBarMenu.frame = CGRectMake(0, 0, 1024, 0);
}];
}
}
try this
if (sideView.frame.origin.x >= 0 )
{
[UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionBeginFromCurrentState animations:^{
sideView.frame=CGRectMake(-400, 45, CGRectGetWidth(sideView.frame), CGRectGetHeight(sideView.frame));
} completion:^(BOOL finished) {
}];
}
else
{
[UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionBeginFromCurrentState animations:^{
sideView.frame=CGRectMake(0, 45, CGRectGetWidth(sideView.frame), CGRectGetHeight(sideView.frame));
} completion:^(BOOL finished)
{
}];
}
I think set frame directly is not a very good method,I suggest to change frame is more appropriate.
Such as:
- (void)menuButton_TouchUpInside:(TopBarIcon *)sender
{
isTopBarMenuShown = !isTopBarMenuShown;
if (isTopBarMenuShown) {
[UIView animateWithDuration:1.5 animations:^{
CGRect rect = topBarMenu.frame;
rect.size.height = 600.0f;
topBarMenu.frame = rect;
//topBarMenu.frame = CGRectMake(0, 64, 1024, 600);
}];
} else {
[UIView animateWithDuration:1.5 animations:^{
CGRect rect = topBarMenu.frame;
rect.size.height = 0.0f;
topBarMenu.frame = rect;
//topBarMenu.frame = CGRectMake(0, 64, 1024, 0);
}];
}
}

Create Continous uiimage animation in x axis

i want to create background like http://disqus.com/ continues background but i can't get it after try with my code like this
continuesBackground =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"pattern"]];
continuesBackground.frame=CGRectMake(0, 56, 320, 102);
continuesBackground.layer.zPosition=100;
[UIView animateWithDuration:5.0
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
continuesBackground.transform = CGAffineTransformMakeTranslation(0, 200); }
completion:^(BOOL finish) {
continuesBackground.transform = CGAffineTransformMakeTranslation(200, 0);
}];
how to make it ? do i need nstimer?
Try this:
- (void)animateImageViewFromStartPosition:(CGPoint)aStartPosition toEndPosition:(CGPoint)aEndPosition; {
CGRect oldFrame = CGRectMake(aStartPosition.x, aStartPosition.y, continuesBackground.frame.size.width, continuesBackground.frame.size.height);
[continuesBackground setFrame:oldFrame];
[UIView animateWithDuration:5.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^(void){
CGRect newFrame = continuesBackground.frame;
newFrame.origin = aEndPosition;
continuesBackground.frame = newFrame;
}
completion:^(BOOL finished){
if(finished){
[self animateImageViewFromStartPosition:aStartPosition toEndPosition:aEndPosition];
}
}];
}
Then call the method in your viewDidLoad method:
- (void)viewDidLoad; {
[super viewDidLoad];
[self animateImageViewFromStartPosition:CGPointMake(0.0f, 56.0f) toEndPosition:CGPointMake(320.0f, 56.0f)];
}
You can easily modify this to a transform, or whatever you want, and it will keep the animation going indefinitely. Hope that Helps!
- (void)animateImageViewFromStartPosition:(CGPoint)aStartPosition toEndPosition:(CGPoint)aEndPosition; {
CGRect oldFrame = CGRectMake(aStartPosition.x, aStartPosition.y,continuesBackground.frame.size.width, continuesBackground.frame.size.height);
[continuesBackground setFrame:oldFrame];
[UIView animateWithDuration:5.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^(void){
CGRect newFrame = continuesBackground.frame;
newFrame.origin = aEndPosition;
continuesBackground.frame = newFrame;
}
completion:^(BOOL finished){
if(finished){
[self animateImageViewFromStartPosition:aStartPosition toEndPosition:aEndPosition];
}
}];
}
Then call the method in your viewDidLoad method:
- (void)viewDidLoad; {
[super viewDidLoad];
[self animateImageViewFromStartPosition:CGPointMake(0.0f, 56.0f) toEndPosition:CGPointMake(320.0f, 56.0f)];
}
talking about this code please let me know where to apply an code for
The following correct answer of the problem in Swift 4.2 version:
func animateImageViewFrom(startPosition aStartPosition: CGPoint, toEndPosition aEndPosition: CGPoint) {
let oldFrame = CGRect(x: aStartPosition.x, y: aStartPosition.y, width: continuesBackground.frame.size.width, height: continuesBackground.frame.size.height)
continuesBackground.frame = oldFrame
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseInOut, animations: {
var newFrame = self.continuesBackground.frame
newFrame.origin = aEndPosition
self. continuesBackground.frame = newFrame
}) { (finished) in
if finished {
self.animateImageViewFrom(startPosition: aStartPosition, toEndPosition: aEndPosition)
}
}
}

Resources