I'm pretty new on iOs programming. And I'm stuck at a (i'm sure) very simple issue.
Don't know what i'm doing wrong...
-My viewDidLoad:
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 768, 1024);
UIView *uno=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainView.image = [UIImage imageNamed:#"photo.jpg"];
[uno addSubview:mainView];
UIView *dos=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainViewDos = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainViewDos.image = [UIImage imageNamed:#"Default.png"];
[dos addSubview:mainViewDos];
//
[self.view addSubview:uno];
//
[self anima:uno:dos];
And my anima method:
-(void) anima:(UIView *)uno:(UIView *)dos{
[UIView transitionFromView:uno
toView:dos
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
It changes the view but without transition...
Thanks
You can't perform an animation within your viewDidLoad--the view changes you make in there are all executed before the view is actually displayed, which is what you're seeing.
Are you trying to show this animation when the view is first displayed? If so, you can get it to work by putting the animation on a timer. Note that with this approach, you'll also have to refactor your anima method a bit to take a single argument.
In your viewDidLoad:
NSDictionary *views = [NSDictionary dictionaryWithObjectsAndKeys:uno, #"uno", dos, #"dos", nil];
[self performSelector:#selector(anima) withObject:views afterDelay:0.1];
Then change your anima method to:
-(void) anima:(NSDictionary *)views {
[UIView transitionFromView:[views objectForKey:#"uno"]
toView:[views objectForKey:#"dos"]
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
Related
- (void)loadView
{
[super loadView];
arrayOfImages = [[NSMutableArray alloc]initWithObjects:#"11.jpg",#"22.jpg",#"33.jpg", nil];
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView];
NSLog(#"%#",[arrayOfImages objectAtIndex:0]);
}
When I put the NSMutableArray in -(void)viewDidLoad, UIImageView displays nothing and NSLog shows NULL. Why is that?
ps. NSMutableArray worked perfectly in -(void)loadView. I've declared NSMutableArray *arrayOfImage in #interface .h file
The only way that the given could would output "NULL" in this situation is that arrayOfImages is NULL by itself.
This is only possible if arrayOfImages is declared as a weak variable.
But as #maddy pointed out, your code is all wrong: Don't call super, but assign self.view. Or use viedDidLoad instead (probably what you want here).
-(void)loadView
{
[super loadView];
arrayOfImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"11.jpg"],[UIImage imageNamed:#"22.jpg"],[UIImage imageNamed:#"33.jpg"], nil];
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView];
NSLog(#"%#",[arrayOfImages objectAtIndex:0]);
This will work but you will only get to see 1 picture. ie the picture in the 0 index of the array.
If you want to show all then you got to apply a loop.
Alright so i have animations i've pulled from previous apps i have developed that all worked on previous versions of iOS, however the iOS 7 is causing me problems
The weirdest thing is this works perfectly in the ViewController.m (main view) but not in the game.m
for now i've been dropping it in the viewDidLoad. In the game view controller it will load the image but it will skip the whole animation so it'll end up at 0,0 as soon as the view loads. in the view controller it'll have the 3 second delay then take 10 sec to animate.
UIImage *bg1 = [UIImage imageNamed:#"bgImage.png"];
UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 568, 320, 568)] initWithImage:bg1];
[self.view addSubview:background];
[UIView animateWithDuration:10.0
delay:3.0
options:UIViewAnimationOptionCurveLinear animations:^{
[background setFrame:CGRectMake(0, 0, background.frame.size.width, background.frame.size.height)];
}
completion:nil
];
So my question stands at Why does it work in the ViewController but not GameViewController?I have a feeling it has to do with UIView, so what should be put there?
added to header
UIImageView *background
Revised code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImage *bg1 = [UIImage imageNamed:#"bgImage.png"];
background = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 568, 320, 568)] initWithImage:bg1];
[self.view addSubview:background];
}
-(void)viewDidAppear:(BOOL)animated {
[UIView animateWithDuration:10.0
delay:3.0
options:UIViewAnimationOptionCurveLinear animations:^{
[background setFrame:CGRectMake(0, 0, background.frame.size.width, background.frame.size.height)];
}
completion:nil
];
}
I am trying to Display a an Image when the user Taps on the UIImageView. But before the user Taps the UIImageView the Image should not be shown, and after a few seconds the Image should disappear again. Does anyone know how to do this? I read through couple of Threads but they do not work with the latest Xcode as it appears. Thanks for your help and time.
Udate
Well, my code now looks like this:
-(void)imageTapped:(UITapGestureRecognizer*)recognizer
{
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = [UIImage imageNamed:#"twingo_main.png"];
[UIView animateWithDuration:1.0 delay:2.0 options:0 animations:^{
recognizer.view.alpha=1.0;
} completion:^(BOOL finished) {
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = nil;
recognizer.view.alpha=1.0;
}];
}
- (void)viewDidLoad
{
UIImageView *hiddenImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 20, 20)];
hiddenImage.userInteractionEnabled=YES;
[hiddenImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)]];
[self.view addSubview:hiddenImage];
Well, now my question is, how do I need to set up the UIImageView in the View Controller?
In your UIViewController's viewDidLoad method:
...
UIImageView *hiddenImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 20, 20)];
hiddenImage.userInteractionEnabled=YES;
[hiddenImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)]];
[self.view addSubview:hiddenImage];
...
UITapGestureRecognizer Handler:
-(void)imageTapped:(UITapGestureRecognizer*)recognizer
{
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = [UIImage imageNamed:#"imageName"];
[UIView animateWithDuration:1.0 delay:2.0 options:0 animations:^{
recognizer.view.alpha=1.0;
} completion:^(BOOL finished) {
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = nil;
recognizer.view.alpha=1.0;
}];
}
Set the image in the UIImageView to nil initially. Add a tap gesture recognizer to the UIImageView that, when fired, sets the image and starts a timer. When the timer completes, set your image back to nil.
Here is an another way to handle above with NSTimer..
-(void)imageTapped:(UITapGestureRecognizer*)recognizer
{
CustomPopUpView *lCustomPopUpView = [[CustomPopUpView alloc]init];
//Added your imageview to Custom UIView class
[self.window addSubview:lCustomPopUpView];
[self.window bringSubviewToFront:lCustomPopUpView];
mPopupTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(closePopUp) userInfo:Nil repeats:FALSE];
}
- (void)closePopUp{
if (mPopupTimer != nil) {
[mPopupTimer invalidate];
mPopupTimer = nil;
}
for (UIView *lView in self.window.subviews) {
if ([lView isKindOfClass:[CustomPopUpView class]]) {
[lView removeFromSuperview];
}
}
}
Its better to have view behind the UIImageView to handle the TapGesture.
But here is the fix for you code:
Add this to viewDidLoad method to setup you image view to handle tap gesture
//By default the UserInteraction is disabled in UIImageView
[self.testImageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapgesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(ShowImage:)];
tapgesture.numberOfTapsRequired=1;
tapgesture.numberOfTouchesRequired=1;
[self.testImageView addGestureRecognizer:tapgesture];
here is the method handle the gesture event
-(void)ShowImage:(UIGestureRecognizer*)recognizer{
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = [UIImage imageNamed:#"clone.jpg"];
[UIView animateWithDuration:1.0 delay:0 options:0 animations:^{
recognizer.view.alpha=1.0;
} completion:^(BOOL finished) {
//[self performSelector:#selector(hideImage:) withObject:recognizer.view afterDelay:10];
[UIView animateWithDuration:1.0 delay:3.0 options:0 animations:^{
recognizer.view.alpha=0.0;
} completion:^(BOOL finished) {
((UIImageView*)recognizer.view).image=nil;
((UIImageView*)recognizer.view).alpha=1.0;
}];
}];
}
If you set the alpha of view to 0, then your view will not receive any touch event further. So its best practice to set it again to 1.0 after removed the image from UIImageView.
You may want to consider using a UIButton. Detecting touches with these is easy - as is changing their image.
You could also subclass UIControl (see http://www.raywenderlich.com/36288/how-to-make-a-custom-control).
I am trying to animate a tableview into my view, however I cannot get the data in the UITableView to appear (NAConversationViewController below is a subclass of UITableViewController):
Post *post = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIView *takeover = [[UIView alloc] initWithFrame:self.view.frame];
takeover.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.75];
NAConversationViewController *vc = [[NAConversationViewController alloc] initWithNibName:#"NAConversationViewController" bundle:nil];
vc.managedObjectContext = self.managedObjectContext;
vc.post = post;
vc.view.frame = CGRectMake(0, self.view.frame.size.height, 320, 320);
[takeover addSubview:vc.view];
[self.view addSubview:takeover];
[UIView animateWithDuration:0.2
animations: ^{
vc.view.frame = CGRectMake(0, 100, 320, 320);
}
completion:^(BOOL finished) {
if (finished) {
[vc.view setNeedsDisplay];
}
}];
The only way I can get the table data to show is [vc.view setNeedsDisplay]; but as soon as I scroll on the tableview the table cells instantly disappear.
Am I invoking the UITableView incorrectly?
If you are using ARC, the NAConversationViewController instance is probably being released, and it's view too... Try creating an instance variable so the view controller does not get deallocated.
I am trying to make an animation that it will changes some pictures that i have already in the app.
After searching around i found something that works pretty well for me.
Code below:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.jpg"],
[UIImage imageNamed:#"photo2.jpg"],
[UIImage imageNamed:#"photo3.jpg"], nil];
animationView.animationDuration = 5;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
So even this works great for me, it gives me a logic error (the yellow triangle sign) and it says
"Local declaration of 'animationView' hides instance variable"
Even tho i run the code and it does what i want it to do, i cant understand why i have this error.
Also to note that this takes place inside an UIImage that i have declared as an outlet
And one extra question: How can i add the fadein - fadeout effect ?
Thanks in advance
You must have declared UIImageView *animationView as either an instance variable or a property.
You can safely remove the UIImageView * part in your viewDidLoad as there is no need to re-declare animationView as UIImageView since compiler already knows about it and that's why it gives you the warning.
So it would look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.jpg"],
[UIImage imageNamed:#"photo2.jpg"],
[UIImage imageNamed:#"photo3.jpg"], nil];
animationView.animationDuration = 5;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
As for the fade effect you could do something like:
//Fade in
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[animationView setAlpha:1];
[UIView commitAnimations];
//Fade out
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[animationView setAlpha:0];
[UIView commitAnimations];