I'm adding a background effect to my app by rotating an UIImageView using:
- (void) viewDidLoad {
rotateAngle = 0;
[NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:#selector(bgEffect) userInfo:nil repeats:YES];
}
- (void) bgEffect{
rotateAngle += .01;
CGAffineTransform rotate = CGAffineTransformMakeRotation( rotateAngle );
[_bgImage setTransform:rotate];
}
The weird problem with that is it affects some of my UI elements by shifting them. It doesn't affect all UI elements, just some. I tried to change the hierarchy, but no luck. I don't understand what the problem is. Can someone help me?
Try turning off Auto-Layout in your Storyboard.
Related
I'm trying to create something that if I have a UIView with background of yellow, and this view is moving on the screen, and somewhere placed another UIView with background of red. How can I know if the yellow color touch the red color? - which mean's that the first view touches the another view.
During the animation you will have to periodically check for intersection like #AMI289 has said. e.g.
- (void)animate {
// Use an NSTimer to check for intersection 10 times per second
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(checkForCollision) userInfo:nil repeats:YES];
[UIView animateWithDuration:5 animations:^{
// Do the animation
} completion:^(BOOL complete) {
// Tell timer to stop calling checkForCollision
[timer invalidate];
}];
}
- (void)checkForCollision {
if (CGRectIntersectsRect([viewOne.layer.presentationLayer frame], [viewTwo.layer.presentationLayer frame])) {
// Handle collision
}
}
It is important to get the presentation layer of the views that are being animated otherwise you will not detect incremental changes to the views' positions on the screen. You also need to invalidate the timer once you are done with it otherwise your checkForCollision method will continue to run indefinitely.
I have created a scene where I would like to have the character image move throughout the circle that I have created.
the character is an image view and the box containing the circle is an image view.
as of right now I have the image moving but it is throughout the entire scene. How would I limit the movement to just within the circle?
here is the code that I have so far:
.h
#interface FinalProjectViewController : UIViewController {
IBOutlet UIImageView *romo;
CGPoint pos;
NSTimer *romoMove;
}
-(void)romoMoving;
.m
- (void)viewDidLoad {
romoMove = [NSTimer scheduledTimerWithTimeInterval:0.09 target:self
selector:#selector(romoMoving) userInfo:nil repeats:YES];
pos = CGPointMake(5.0, 4.0);
}
-(void)romoMoving
{
romo.center = CGPointMake(romo.center.x+pos.x, romo.center.y+pos.y);
if (romo.center.x>170||romo.center.x<0) {
pos.x = -pos.x;
}
if (romo.center.y>180||romo.center.y<0) {
pos.y = -pos.y;
}
}
I was thinking the best way to go about it was to find the points of the circle that I would like the character to hit then set those as the points as it should move to. I am not sure if that would be the best way to do this, nor was I very sure on how to accomplish this.
I am trying to create a Calibration View! I have 12 calibration points as UIImageViews. Now I want to show every point for 5 seconds. So the whole calibration time is 1 minute. The Alpha of the ImageViews is set to 0.0! In the UI Animation I want to set the Alpha to 1.0 for one point after an other, for each point only for 5 seconds.
So far I did this (see the code)! But this animates me (fades in) after 5 seconds for 5 seconds all 12 Points at once!How can I solve this for one after another with a NSTimer and UI Animation? Thanks
-(id)init{
_calibrationViewArray = [[NSMutableArray alloc]init];
_calibrationPoint1 = [[UIImageView alloc]initWithFrame:(CGRectMake(115.5,113.0,25.0,25.0))];
_calibrationPoint1.backgroundColor = [UIColor redColor];
_calibrationPoint1.alpha = 0.0;
[self addSubview:_calibrationPoint1];
[_calibrationViewArray addObject:_calibrationPoint1];
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
-(void)onTimer{
for (int i = 0; i < [_calibrationViewArray count]; i++) {
UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:i];
[UIView beginAnimations:#"Calibration" context:nil];
[UIView setAnimationDuration:5.0];
// Make the animatable changes.
currentCalibrationPoint.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
}
}
Declare a variable in your class to be:
int cont;
Initialize variable when is needed:
cont=0;
Change your code to this:
-(void)onTimer{
//Loop is not needed, with the timer and counter is enough
UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:cont];
[UIView beginAnimations:#"Calibration" context:nil];
[UIView setAnimationDuration:5.0];
// Make the animatable changes.
currentCalibrationPoint.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
if(cont>0){
//Hide previous view (here you can make another animation, instead of changing alpha right away)
[_calibrationViewArray objectAtIndex:cont-1].alpha = 0.0;
}
cont++;//+1
}
In your onTimer method you shouldn't be looping over all of the image views, you should have a currentIndex or similar variable so you can just get the next image view and run an animation on it alone.
Do not use timer at all.
Use the:
[UIView animateWithDuration: animations: completion:]
method.
In the animation block fade one of the dots, then in completion block run the animation again.
I'm trying to get my UITableView to slowly scroll downwards at a steady speed, which I can specify. I've used scrollToRowAtIndexPath, but whether or not I choose to animate it, it's an almost instant movement. I'm looking at something that moves much more slowly.
I did wonder whether I could do something with a UIView animation block, moving one indexPath.row at a time, but since my cells are different heights, this would cause uneven animation speed.
Any other ways I can do this?
I got it with this..
[NSTimer scheduledTimerWithTimeInterval: 0.025 target:self selector:#selector(scrollTableView) userInfo:nil repeats:YES];
And..
- (void) scrollTableView {
float w = 1;
CGPoint scrollPoint = self.tableView.contentOffset;
scrollPoint.y = scrollPoint.y + w;
if (scrollPoint.y >= self.tableView.contentSize.height - (self.tableView.frame.size.height - 100) || scrollPoint.x <= -self.tableView.frame.size.height + 924) {
w *= -1;
}
[self.tableView setContentOffset: scrollPoint animated: NO];
}
You can always decrement the contentOffset y axis over time.
Here you go buddy. Just change the animateWithDuration to higher seconds like 1 or 1.5 to see if it achieves the effect you want. Make sure you include QuartzCore library and #import <QuartzCore/QuartzCore.h> in your *.m file. This is as easy as it gets
-(IBAction)modeTableSlowly
{
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
//here tblSimpleTable2 is tableView
[tblSimpleTable2 setFrame:CGRectMake(183, 56, 137, 368)];
} completion:nil];
}
I try to rotate and move a UIImage.
Here is my code :
im = [[UIImageView alloc ] initWithFrame:CGRectMake(160, 240, 100, 100)];
im.image = [UIImage imageNamed:#"image.png"];
[view addSubview:im];
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:#selector(actionIm) userInfo:nil repeats:YES];
-(void) actionIm
{
[im setFrame:CGRectMake(im.frame.origin.x+1 , im.frame.origin.y+1 ,100, 100)];
im.transform=CGAffineTransformMakeRotation (mRotation);
mRotation+=0.1;
}
The result is a very strange behaviour : If I use only the rotation, it works. If I use only the move, it works. But both uses give a strange result (the IUImage "jump" bizzarly).
I red a lot of things about rotation, frame, bounds, center... but nothing that can solve my problem.
Thank you.
Try moving the center instead of changing the frame. I believe setting the frame when the rotation is not Identity does something different.