How do I move a UIToolbar Item? - iphone

I am using a UIToolbar in iOS and am trying to get a new button to 'arrive' by sliding in from the right. Is there a way to directly access the 'position' of the button on the bar? I currently have a messy workaround that is getting close to the desired effect, but i was hoping there was a way to move them using Core Animation, so i could implement the 'EaseInEaseOut' type timing functions. Any ideas?
- (void) testPart1 {
[NSTimer scheduledTimerWithTimeInterval:(0) target:self selector:#selector(testPart2:) userInfo:[NSNumber numberWithInt:500] repeats:NO];
}
- (void) testPart2:(NSTimer*)sender {
int countdown = [sender.userInfo integerValue];
theSpace.width = 10+(int)countdown;
itemTestButton.width = 49;
itemTestButton.width = 50;
countdown -= 5;
if(countdown>0)
[NSTimer scheduledTimerWithTimeInterval:(0.004) target:self selector:#selector(testPart2:) userInfo:[NSNumber numberWithInt:countdown] repeats:NO];
}
The premise here was using an NSTimer to change the width of a Fixed Space Bar Button item, and count down to zero. I found I had to change the width of an actual button to make it work, otherwise no 'animation' would occur. It's messy I know, someone must have a cleaner way.

I would suggest adding a plain UIView as a subview of the UIToolbar that is rendered exactly as the bar button would be (you could get this by calling renderInContext: on an existing button) and then animate that view into the proper position. In the completion block of that animation you could then set the real button and removeFromSuperview the animation view, and it will look indistinguishable to the user.

Related

Display StopWatch Timer animated like the petrol pump meter using NSTimer

I am new in iOS developement.When I press the stopwatch start button I want to display the timer like counter token effect.I have attached image for your reference.I have done to display secs and minutes but I dont know, How animate autoscroll effect? How can I do this?
When the counter is moving it shouldn't jump from one number to another number, instead it should move from one number to next number smoothly, just like the petrol pump meter. Thanks
I have done something like this before - this code is not necessarily clean, but it does the job.
You want to create twoUILabels in your .h file:
IBOutlet UILabel *figureLabel1;
IBOutlet UILabel *figureLabel2;
Then you want to create a BOOL so that we can tell which UILabel is visible to the user.
BOOL firstLabel;
So lets imagine that the initial label (showing the number 1) is figureLabel1 and the future UILabel to be displayed (showing the number 2) is figureLabel2. However when the counter adds one, then the figureLabel2 moves up and takes the figureLabel1's place. Then, while the figureLabel1 is hidden, it takes the place of figureLabel2 when figureLabel1 was visible.
See here:
// Check what label is showing
if (firstLabel == YES) {
// Following code the hide and move the figureLabel1
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate: self]; //or some other object that has necessary method
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
// Slowing fade out the figureLabel1
[figureLabel1 setAlpha:0];
// Move the figureLabel1 up and out of the way
[figureLabel1 setFrame:CGRectMake(20, 108, 287, 55)];
[UIView commitAnimations];
// Following code the show and move the figureLabel2
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
// Slowing fade in the figureLabel2
[figureLabel2 setAlpha:1];
// Move the figureLabel2 up and into position
[figureLabel2 setFrame:CGRectMake(20, 141, 287, 55)];
[UIView commitAnimations];
// Update BOOL for next label
firstLabel = NO;
} else {
// Exactly the same but opposite
}
As I said, this is not pretty but it shows the basic concept. All the best!
You need to manually scroll tableView instead of scrollToRowAtIndexPath because this animation uses its own timer interval and its very difficult or we can say impossible to change its time interval.
So, I am Implementing an API for such kind of problems and made a demo app for you with smooth scrolling as you want.
You need to use an outer timer that fires every 1 second and an internal timer that will fire every 0.03 sec as my tableRow Height is 30 I calculated internal timer interval as :---
Move 30 pixels for 1 sec , then
Move 1 pixel for 0.33 sec inside internal timer.
The Internal timer will invalidate and fire every 1 second as initialized within outer timer.
And internal timer will perform the movement of cell.
dial4 is a tableView
Have a look at my code.
#define rowHeight 30
-(void)startCounter
{
outertimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(snapCell) userInfo:nil repeats:YES];
}
-(void)stopCounter
{
[outertimer invalidate];
}
-(void)snapCell
{
NSLog(#"Initialize Internal timer %i",secLsb);
secLsb++;
if (secLsb==10) {
[dial4 setContentOffset:CGPointMake(0, 0) animated:NO];
secLsb=0;
NSLog(#"dial content offset y is %f",dial4.contentOffset.y);
}
[internaltimer invalidate];
internaltimer=[NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:#selector(automaticScroll)
userInfo:nil
repeats:YES];
}
-(void)automaticScroll
{
NSLog(#"val is & y ======== %f",dial4.contentOffset.y);
[dial4 setContentOffset:CGPointMake(dial4.contentOffset.x,dial4.contentOffset.y+1) animated:NO];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return rowHeight;
}
Have a look at Time Counter
From image it can be assume that you are trying to get an effect like count-down timer. Have a look at countdown timer here. It may helps you somehow.
Enjoy Programming !!
Refer both links for your output. It helps you
UIPickerView that looks like UIDatePicker but with seconds
and
How can I make my UIPickerView show labels after the selected value?
Thanks.
Several good ideas here so far, but I'll add another.
First, be sure to set the container view (your blue rectangle) to clip children, using the Clip Subviews checkbox in Interface Builder.
Second, add a set of child views with images of each numeral for each digit to be presented (4 * 10 = 40 child views in your case). Use tags in IB so you can easily access each one. Set the initial bounds to just below your bottom margin.
Call UIView animateWithDuration. In the animations block, set the new digit view's frame to appear in the clipped parent view's bounds, and set the old digit view's frame to just above the container's bounds. Since both view's frame changes are animated in the same block, this will create the effect of the new digit sliding up into place as the old digit slides out the top.
In the completion block, set the old digit's frame back to the original position below the container view.
With this approach, you can play with the duration and the timing curves, so that you cam emulate the pause that occurs with each digit fully displayed between transitions.
A similar approach can also be used with CALayer, or with sprites, but UIViews are lightweight, perform well, and easiest to assemble in Interface Builder.

How to programmatically move a slider at a set duration using IOS 6?

I have an iPhone application with a UISlider. When my user presses the play button, I want my slider to move from a value of 1, slowly, to a value of 100. The slider slides for the duration of the recording that gets played back when my user presses the play button.
I looked at the UISlider documentation. The only method that seems like it might help me is the setValue:animated: method. However, when animated=yes, the slider moves to value 100 way too fast.
Any way to slow it down a little?
You can let the UIView animate itself rather than working out the tweening yourself, or writing callbacks. For Example, this animates the slider to it's maximum value from it's current value in 5 seconds. And it's a lot smoother than trying to do it yourself.
- (IBAction)play:(id)sender {
[sender setEnabled:NO];
[UIView animateWithDuration:5.0 animations:^{
self.slider.value = self.slider.maximumValue;
}];
}
If you don't believe me - here's a sample project that you can download to see it for yourself:
You should use scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: which is part of NSTimer to increment the slider more slowly. Increase the position of the slider on every tick. Something like this:
NSTimer *timerForSlider = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateSlider) userInfo:nil repeats:YES];
-(void)updateSlider {
[slider setValue:slider.value+1];
if(slider.value == MAXIMUM_VALUE) {
[timerForSlider invalidate];
}
}

create uiimageView near and outside the screen sides

Hi everyone I'm french so scuse me for my english. well what I want to do is to create every second a new image at a random position, but a random position outside the screen, and(very important) near of the screen sides(the upper side or left side or right or down). like that when I want to animate them, there will be created outside the screen but just around the sides. How can I do this please ?
So basically, my understanding about your question is that you one goal: You want to animate an object via translation.
So the animation goes from outside the visible screen to the viewable.
Here's an approach:
1. You need to determine what are the limits of your "Outside" screen. Depending on the size of your UIImageView frame, 100 px padding is fine I think. So that would be x: (-100, 420), y: (-100, 580).
Generate random number. arc4random() is better than rand() in this case.
Create a method that will be called by an NSTimer in which the UIImageView's are created.
Don't forget to properly manage your UIImageView's. Frequent creating objects without releasing will cause your app to crash. If possible, reuse them instead of creating them each time the method is called.
What this code does is use random:min:max to generate a random x and y value between the desired min and max range. In the onTimer function we allocate a new UIImageView object and then set its center property to be a random location off the screen. If the generated random number inside the if statment is less than 5 then the view will be added to either to the left or top of the screen, otherwise it will be added to the right or bottom.
/*****************************************************************************
Generates a pseudo random CGPoint point value within the min and max range.
*****************************************************************************/
-(CGFloat)rand:(CGFloat)min:(CGFloat)max{
float difference = max - min;
return (((CGFloat) rand()/(CGFloat)RAND_MAX) * difference) + min;
}
-(void)viewDidLoad{
//setup a timer property in your view controller header file
timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:#selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(void)onTimer{
UIImagView *myRandomImageView;
myRandomImageView = [[UIImageView alloc] initWithImage:#"MyRandomImage.png"];
if([rand:1:10] > 5)
//put the image view to top or left side of the screen
myRandomImageView.center = CGPointMake([rand:-100,-200], [rand:-100,-200]);
else
//put the image view to the bottom or right side of the screen
myRandomImageView.center = CGPointMake([rand:420,520], [rand:580,680]);
//dont forget that the values I have eneter here to be passed to the rand:min:max
//function are meant to serve as a guide and that you should enter your own values to
//achieve the desired random view positioning off screen.
[self.view addSubview:myRandomImageView];
[myRandomImageView release]
}

Help scrolling through a UIScrollView

I am placing array of images in a scrollview.
I need to autoscroll the scrollview. I am using the timer and increasing the position of scrollview.
My code looks like this.
- (void)autoscrollmethod {
Timer = [NSTimer scheduledTimerWithTimeInterval:7.5 target:self selector:#selector(moveRect) userInfo:nil repeats:YES];
}
- (void)moveRect {
NSLog(#">>>>>>>> %d",i);
[gallery scrollRectToVisible:CGRectMake(i,0,100,100) animated:NO];
if (i==(arraycount+1)*100) {
[gallery scrollRectToVisible:CGRectMake(0,0,100,100) animated:NO];
i=200;
}
else {
i=i+100;
}
}
It working fine,but I have a problem.
I am scrolling the view to position 800, but scrollRectToVisible:CGRectMake is at 600,then scrollview is not getting back to 600 position,and not autoscroll up to 800.
After 800 it will automatically scrolls normally.
How can I fix this?
Generally speaking, you should use the contentOffset property of the UIScrollView instead of manually using your own increment variable. Contentoffset changes depending on your scrolling and automatically changes itself.

how do I duplicate animation from iPhone photos app

I am building an app that has picture viewing capabilities. I have wrestled the UIScrollView beast to the ground (hope to post my scrollview knowledge RSN) and am now trying to duplicate some of the other visual effects of the iPhone photos app. specifically, when viewing an image, I would like to dissolve the controls and bars away.
I have a method that toggles visibility of the status bar, navigation bar, and tab bar, leaving just the scrollview with an image. The scrollview is full screen. I have a timer that fires 3 seconds after the user does something (single taps the screen, views the next image, etc.) and in the timerFired method, I call my method to turn off the bars. a single tap of the screen turns on the bars. here's the method to toggle the full screen state.
- (void)setFullScreen:(BOOL)fullScreen {
// reset the timer
[myTimer invalidate];
[myTimer release];
myTimer = nil;
// start animation section
[UIView beginAnimations:nil context:nil];
// toggle the status bar
[[UIApplication sharedApplication] setStatusBarHidden:fullScreen animated:YES];
[UIView setAnimationDuration:UINavigationControllerHideShowBarDuration];
CGFloat alpha = fullScreen ? 0.0 : 1.0;
// change the alpha to either 0 or 1 based on hiding or showing the bars
self.navigationController.navigationBar.alpha = alpha;
self.tabBarController.tabBar.alpha = alpha;
// do the animations!
[UIView commitAnimations];
// restart the timer after we show the bars
if (!fullScreen) {
myTimer = [[NSTimer timerWithTimeInterval:3.0 target:self selector:#selector(timerFired:) userInfo:nil repeats:NO] retain];
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
}
}
This basically works, BUT, it doesn't look as good as the photos app. I am animating the alpha values as I believe that this looks more like the photos app instead of the using the hidden with Animation methods available. My problem is doing the same for the status bar.
my questions:
(1) is there a UIView for the the status bar?
(2) is there a way to change the alpha property of the status bar?
(3) is the statusbar in another UIWindow?
(4) is there another way to achieve this using "legal" methods (I need to put this app in the app store)?
I've dumped the windows property of the UIApplication and there was only 1 window and I've crawled the view hierarchy and there was not an obvious view for the status bar.
any ideas?
The status bar is created by SpringBoard itself, it is of class SBStatusBar (which IIRC inherits from UIWindow). It is not accessible to app store applications.