I have a UIView called clouds. It currently moves to the right of the screen, and once all of it has left, it reappears on the left side.
How do I make it re-appear on the left side as it leaves on the right? So when it begins to leave on the right side, the bit which just disappeared reappears on the left side.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
movement = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(moving) userInfo:nil repeats:YES];
cloudsMovement = 2;
}
-(void)platformMovement{
clouds.center = CGPointMake(clouds.center.x + cloudsMovement, clouds.center.y);
}
-(void)moving{
if (clouds.center.x < -11){
clouds.center = CGPointMake(330, clouds.center.y);
}
if (clouds.center.x > 330){
clouds.center = CGPointMake(-11, clouds.center.y);
}
[self platformMovement];
}
Any help would be much appreciated, thanks!
It seems a bit unusual to answer my own question, but to help any other beginners like myself, this is the solution I came up with:
Create another UIView (I called this clouds2).
Move it off-screen to the left, and animate it as the original UIView (clouds1).
Adjust the numbers a bit so when the WHOLE of the image leaves the screen, it re-appears on the left. Kind of like a converyor belt system.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
movement = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(moving) userInfo:nil repeats:YES];
cloudsMovement = 2;
}
-(void)platformMovement{
clouds1.center = CGPointMake(clouds1.center.x + cloudsMovement, clouds1.center.y);
clouds2.center = CGPointMake(clouds2.center.x + cloudsMovement, clouds1.center.y);
}
-(void)moving{
if (clouds1.center.x > 470){
clouds1.center = CGPointMake(-150, clouds1.center.y);
}
if (clouds2.center.x > 470){
clouds2.center = CGPointMake(-150, clouds2.center.y);
}
[self platformMovement];
}
Related
I want to move my UIView of size (x=50,y=50) in the boundary of simulator screen means rectangular without animation infinitely.
To moving view continuously in your simulator boundary you have to put logic there is no predefined method without animation. if you are using animation then below given link is useful. How to repeat animation from current state not from startingstate? or without animation the below code will help you.
And yes please note that **
pos
** is a object of CGPoint class and you don't have to allocate it simply declare in interface.
- (void)viewDidLoad
{
pos = v1.center;
NSLog(#"POSITION === %f",pos.x);
[NSTimer scheduledTimerWithTimeInterval:0.05
target:self
selector:#selector(rotateBoundary)
userInfo:nil
repeats:YES];
[super viewDidLoad];
//[self performSelector:#selector(rotateBoundary) withObject:self afterDelay:5.0];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)rotateBoundary
{
if (pos.x>=20 && pos.x<=290 && pos.y==25)
{
NSLog(#"Top left to right.");
pos.x+=5;
v1.center = pos;
NSLog(#"pos.x === %f",pos.x);
}
if(pos.x >= 290 && pos.y<=430)
{
NSLog(#"Right top to down.");
pos.y+=5;
v1.center = pos;
NSLog(#"pos.y === %f",pos.y);
}
if(pos.y == 430 && pos.x>=20)
{
NSLog(#"Down right to left.");
pos.x-=5;
v1.center = pos;
NSLog(#"pos.x === %f",pos.x);
}
if (pos.x <= 20 && pos.y>=25)
{
NSLog(#"Down left to top.");
pos.y-=5;
v1.center = pos;
NSLog(#"pos.y === %f",pos.y);
}
}
I have a set of 4 images that i want to show in a UIScrollView. This scroll view contains ad banners. So they have to keep scrolling vertically down automatically similar to marquee effect - user does not need to touch the screen to scroll.This process should keep repeating.
If the user touches the screen, the scrolling should stop & when user re-touches the screen the scrolling should restart from where it had left.
There is as simpler way, I guess. UIImageView can help you. Follow these Steps :
1) Add 4 UIImageViews Vertically Down.
2) Create 4 Different Arrays for these ImageViews.
NSArray *frames1 = [NSArray arrayWithObjects:[UIImage imageWithName:#"a.png"],[UIImage imageWithName:#"b.png"],[UIImage imageWithName:#"c.png"],[UIImage imageWithName:#"d.png"],nil];
NSArray *frames2 = [NSArray arrayWithObjects:[UIImage imageWithName:#"b.png"],[UIImage imageWithName:#"c.png"],[UIImage imageWithName:#"d.png"],[UIImage imageWithName:#"a.png"],nil];
NSArray *frames3 = [NSArray arrayWithObjects:[UIImage imageWithName:#"c.png"],[UIImage imageWithName:#"d.png"],[UIImage imageWithName:#"a.png"],[UIImage imageWithName:#"b.png"],nil];
NSArray *frames4 = [NSArray arrayWithObjects:[UIImage imageWithName:#"d.png"],[UIImage imageWithName:#"a.png"],[UIImage imageWithName:#"b.png"],[UIImage imageWithName:#"c.png"],nil];
3) Provide these Arrays to all these different ImageViews.
yourImageView1.animationImages = frames1;
yourImageView2.animationImages = frames2;
yourImageView3.animationImages = frames3;
yourImageView4.animationImages = frames4;
4) You can add some Effects also...
// all frames will execute in 1.75 seconds
yourImageView.animationDuration = 1.75;
// repeat the annimation forever
yourImageView.animationRepeatCount = 0;
5) Simply startAnimating the ImageViews.
[yourImageView1 startAnimating];
[yourImageView2 startAnimating];
[yourImageView3 startAnimating];
[yourImageView4 startAnimating];
6) You can always use -touchesEnded method to start and stop Animation.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([[touches anyObject] view] == yourImageView1) {
//Here you can write the code to stop and start Animation of UIImageView
}
}
I think this will give you the effect what you want.
GoodLuck !!!
This is how I have done it finally :-) .
Call the below method from viewDidLoad
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(scrollTheScrollView) userInfo:nil repeats:YES];
Now implement the method
-(void)scrollTheScrollView{
static int i=0;
i++;
if(self.scrollView.contentOffset.y == 700.0)//This 700.0 will be different for you
i=0;
NSLog(#"Content offset in scrolling method is %#",self.scrollView);
[self.scrollView setContentOffset:CGPointMake(0, i*5)];}
I made an API for this, if you're interested.
https://github.com/dokun1/DOMarqueeLabel
I am working on app in which i have to hide control button after 3 second so i write code using NSTimer and Touch began and it work but problem is that when i touch again with on any another button than my timer is not reset and even if i move my touch example like drag.
If i drag or move touch it should reset the timer but it won't.
I found that this implementation work if i continuously touch on other area (but it is not working on Control button) IF i continuously touch control button still it goes hidden after 3 second.How to solve this problem. i want event to fire on button click also.
EDITED I solve my problem by own..
I have added this code on button click sector and it works..
Thank you for all support
if(screenTimer)
{
[screenTimer invalidate];
screenTimer = nil;
screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(turnOffScreen) userInfo:nil repeats:NO];
}
Here is my code
// Touch began for touch event.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(screenTimer)
{
[screenTimer invalidate];
screenTimer = nil;
}
mode1View.hidden=NO;
screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(turnOffScreen) userInfo:nil repeats:NO];
}
- (void)turnOffScreen{
NSLog(#"TURN OFF SCREEN");
if(screenTimer!=nil)
{
mode1View.hidden=YES;
}
}
Any help is appreciated. Thank you
have you tried
[self performSelector:#selector(turnOffScreen:) withObject:nil afterDelay:3.0];
The NSObject method performSelector:withObject:afterDelay: it invoke a methods on the object with an objec after a certain delay.
I have a problem with google maps in ios. I have a screen with the MapView and I am drawing some annotations (polygones dispalying zones on the streets). If somebody taps the zone it gets selected a button shows up and the user can press it to start a transaction. He has to hold the button for 2s and in this time a small view with a progress bar is shown to indicate that the user has to hold it till it comes to end. This works fine. However sometimes after playing a bit following problem appears:
The View with the progress bar show up but disappears before it reaches the end. If try next time everything works smoothly. Here is the code that matters, I think:
- (IBAction)starButtonTouched:(id)sender {
self.progressLabel.text = [NSLocalizedString(#"Please hold", nil) uppercaseString];
if (!self.currentVehicle) return;
self.progressBar.hidden = NO;
self.progressBarBackground.hidden = NO;
self.progressBar.progress = 0;
[self performSelector:#selector(_updateProgress) withObject:nil afterDelay:kVTMainButtonPressDuration/kVTMainProgressSteps];
}
- (IBAction)startButtonReleased:(id)sender {
NSLog(#"StartButton released - progress bar will be hidden");
self.progressBar.hidden = YES;
self.progressBarBackground.hidden = YES;
}
- (void) _updateProgress {
if (self.progressBar.hidden) return;
/*
* update progress bar
*/
float actual = [self.progressBar progress];
if (actual < 1) {
self.progressBar.progress += 1.0/kVTMainProgressSteps;
[NSTimer scheduledTimerWithTimeInterval:kVTMainButtonPressDuration/kVTMainProgressSteps target:self
selector:#selector(_updateProgress) userInfo:nil repeats:NO];
/*
* if it has ended start the transaction
*/
} else {
ASLogInfo(#"Starting");
if (!self.currentVehicle) return;
[self.service startParking:self.currentVehicle zone:self.selectedZone callback:^(VTTransaction *transaction) {
[self loadCurrentTransaction];
}];
}
}
Does anybody have an idea what and why causes the View to disappear? Thanks a lot.
Bye,
Filip
What I am trying to achieve is showing a view during a couple of seconds without user intervention. Is the same effect as the ringer volume view that appears when pressing the volume controls on iphone:
I have a scroll view with an image, taping in the image, sound begin to play, another tap and it pauses. I would like to implement the above effect just to inform of the action (showing a play/pause images).
I hope that I have explained the problem perfectly.
Many thanks for your help.
Regards
Javi
Assume you have some class inherited from UIViewController. You can use the code below:
const int myViewTag = 10001;
const int myInterval = 1; // define the time you want your view to be visible
- (void)someAction {
//this could be your `IBAction` implementation
[self showMyView];
[NSTimer scheduledTimerWithTimeInterval:myInterval
target:self
selector:#selector(hideMyView)
userInfo:nil
repeats:NO];
}
- (void) showMyView {
//you can also use here a view that was declared as instance var
UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(100, 100, 120, 120)] autorelease];
myView.tag = myViewTag;
[self.view addSubview:myView];
}
- (void) hideMyView {
//this is a selector that called automatically after time interval finished
[[self.view viewWithTag:myViewTag] removeFromSuperview];
}
You can also add some animations here but this is another question :)