I'm having a problem with UI animations.
I have a uiview: theView.
I animate theView to move to some random position on the mainView. It moves from point A to point B and changes its scale from 1.0 to 1.5;
So after the animation, the view is bigger and in a new position.
I call a second animation, by button. This one only move theView.
After the animation, the view has moved. However, the view is no longer bigger, its scale is back at 1.0.
So do these animation values not become theViews new values for size,position,ex?
I will post code soon, because it of course may be a careless error I am missing.
If its UIView animation.
- (void)viewDidLoad
{
[super viewDidLoad];
_yourView = [[UIView alloc]initWithFrame:CGRectMake(300, 300, 100, 100)];
[_yourView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_yourView];
[UIView animateWithDuration:1.0f animations:^{
[_yourView setFrame:CGRectMake(400, 400, 150, 150)];
} completion:nil];
UIButton *aMoveButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 5.0f, 60.0f, 40.0f)];
[aMoveButton setTitle:#"MoveView" forState:UIControlStateNormal];
[aMoveButton addTarget:self action:#selector(moveYourView) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:aMoveButton];
}
-(void)moveYourView{
[UIView animateWithDuration:1.0f animations:^{
[_yourView setFrame:CGRectMake(300, 500, 150, 150)];
} completion:nil];
}
Believe me this works and it actually changes the view's values for size,position.
I cannot post a comment, so I would just like to suggest that you say UIView, i'm not 100% sure, so dont take my word for it, but im pretty sure you should use UIImageView instead. Hope that helps a little :)
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"campFire01.gif"],
[UIImage imageNamed:#"campFire02.gif"],
[UIImage imageNamed:#"campFire03.gif"],
[UIImage imageNamed:#"campFire04.gif"],
[UIImage imageNamed:#"campFire05.gif"],
[UIImage imageNamed:#"campFire06.gif"],
[UIImage imageNamed:#"campFire07.gif"],
[UIImage imageNamed:#"campFire08.gif"],
[UIImage imageNamed:#"campFire09.gif"],
[UIImage imageNamed:#"campFire10.gif"],
[UIImage imageNamed:#"campFire11.gif"],
[UIImage imageNamed:#"campFire12.gif"],
[UIImage imageNamed:#"campFire13.gif"],
[UIImage imageNamed:#"campFire14.gif"],
[UIImage imageNamed:#"campFire15.gif"],
[UIImage imageNamed:#"campFire16.gif"],
[UIImage imageNamed:#"campFire17.gif"], nil];
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];
Related
I create a UIImageView and add to VC UIView , then I create a image with animatedImageWithImages:duration:
When I execute the code, it works normally. It displays image animation. But when I push to next VC and in the process of swipe back , I find the UIImageView display nothing.
At the moment finger left screen , everything back to normal.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
[self.view addSubview:imgView];
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
imgView.image = [UIImage animatedImageWithImages:imgs duration:0.5];
}
BugGif
I notice the imageView has CAKeyframeAnimation. I guess the conflict that runmode and CAKeyframeAnimation.
SDWebImage has the same problem. Because it also use the method that create UIImage. I think the point is the method of animatedImageWithImages:duration: and CAKeyframeAnimation.
Anyone knows the solution? Thanks a lot.
The event that the finger left the screen will make imageView display normally. Why and How to resolve?
GitHubBugDemo
You can use UIImageView another methods to set images and animation duration as below
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
[imgView setAnimationImages:imgs];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount:HUGE_VAL];
[imgView startAnimating];
[self.view addSubview:imgView];
In my app, I have an array of image, which is getting animated for every second.
I used the following code,
animateArrayImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"gallery-img-1.png"],
[UIImage imageNamed:#"gallery-img-2.png"],nil];
animateImageView.animationImages = animateArrayImages;
animateImageView.animationDuration = 1.0f;
animateImageView.animationRepeatCount = 0;
[animateImageView startAnimating];
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(20, 20, 20, 20)];
[self.view addSubview:button];
But I am getting an output like this
The button is getting displayed in the background of the image.
But I want to display it befor the image
If you want to bring the button on top of the image, you can call the following method after addSubview:
[self.view bringSubviewToFront:button];
Hi I am still a beginner with Objective-C and am hoping someone can help me out. What I have created programmatically is a UIScrollView that has images inside that you can swipe across. Then I created a UIButton that when pressed is supposed to take a screenshot of the image subview the has been selected and then saves it to the camera roll (that part i'm not sure where to add in the code). The reason I want it to be a screenshot and not just the image is because I will later add UILabels on top of the images and I want those to appear in the saved screenshot as well. I am not quite sure which views to use in the saveWallpaper method and even if my current solution will work. I have looked at this answer Link to get the screenshot code to try for this implementation. Basically I want it to take a screenshot of everything besides the UIButton and save it to the camera roll. Any help or direction would be greatly appreciated. I could also try an approach where it takes a screenshot or captures certain elements and adds them together to be a single image. Thank you!
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 21;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png",#"4.png",#"5.png",#"6.png",#"7.png",#"8.png",#"9.png",#"10.png",#"11.png",#"12.png",#"13.png",#"14.png",#"15.png",#"16.png",#"17.png",#"18.png",#"19.png",#"20.png",#"21.png", nil];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
scroller.pagingEnabled = YES;
scroller.bounces = NO;
[self.view addSubview:scroller];
int width=scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[scroller addSubview:ImgView];
scroller.contentSize = CGSizeMake(width, 0);
width +=scroller.frame.size.width;
xPos +=scroller.frame.size.width;
}
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[saveButton addTarget:self
action:#selector(saveWallpaper:)
forControlEvents:UIControlEventTouchDown];
[saveButton setImage:[UIImage imageNamed:#"save.png"] forState: UIControlStateNormal];
[saveButton setImage:[UIImage imageNamed:#"savepressed.png"] forState: UIControlStateHighlighted];
saveButton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2, 396, 96.5, 42); //Make this in the center
[self.view addSubview:saveButton];
}
- (void)saveWallpaper:(id)sender
{
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(scroller.bounds.size, NO, [UIScreen mainScreen].scale);
}
else
{
UIGraphicsBeginImageContext(scroller.bounds.size);
[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:#"foo.png" atomically:YES];
}
}
Rather than drawing the complete scroller view as in the following statement
[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];
Draw the selected image and the text using drawInRect method. See this related post for reference.
I have one menubar as a uiimage. I want to make 5 menu on this menubar are clickable and link to another view. Can anyone tell me the way i can do that? I found people using uibutton on the uiimage, Is that fine for me and how can i separate the menu bar to 5 pieces.
Thanks for both rmaddy and Brent Royal-Gordon
I decided to use the uiimage as a menubar background and add the uibutton on it. i try some thing like this:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"MOVIE" ofType:#"MP4"]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.repeatMode = MPMovieRepeatModeOne;
moviePlayer.shouldAutoplay = YES;
moviePlayer.scalingMode = MPMovieScalingModeFill;
[moviePlayer.view setFrame:CGRectMake(0,0,1330,320)];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setContentSize:CGSizeMake(1330,320)];
[scrollView setScrollEnabled: YES];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
[scrollView addSubview:moviePlayer.view];
[self.view addSubview:scrollView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:image.frame];
UIImage *menubar = [UIImage imageNamed:#"BG.jpg"];
imageView = [[UIImageView alloc] initWithImage:menubar];
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.frame = CGRectMake(0, 258, imageView.frame.size.width, 25);
[self.view addSubview:imageView];
[imageView setUserInteractionEnabled:YES];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,258,30,25)];
[btn addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"HOME" forState:UIControlStateNormal];
[imageView addSubview:btn];
but the button is not visible on screen when i run on the simulator. What did i do wrong?
Using a gesture recognizer is a bad idea because:
It forces you to interpret the touch areas for each submenu yourself.
It's opaque to VoiceOver.
It doesn't show a highlight when the user touches a button. These sorts of highlights help the user understand what the user interface is doing, so they're very valuable.
What you should do is split the single image into five in a graphics tool like Photoshop, and make each of these slices the image of a UIButton. You'll need to design highlighted states for these images (shown when one of them is held down), but you should be doing that anyway.
If you must slice the image in code, you can do that by using UIGraphicsBeginImageContextWithOptions(size, NO, 0.0) and drawing your big image using drawAtPoint: with an appropriate offset to get the slice you want, but 99.99999% of the time, it'll make more sense to slice the images once on your big, beefy, wall-plug-powered Mac than every time your app runs on a tiny, slow, battery-powered iOS device.
Use a UITapGestureRecognizer with the UIImageView containing your image. You will need to set the userInteractionEnabled property to YES on the image view.
When you get the tap event, look at where in the view the tap occurred. Based on the location, perform one of the five menu actions.
If you want more control, you may be better off splitting the image into 5 and creating 5 UIButton instances instead.
Why you add it the buttons to image view ? Just adding it to the View. It will solve the problem(as i understand).
[imageView addSubview:btn];
Try like this
[self.view addSubview:btn];
Hope this helps
issue is with the Width of the button.Try
this
[btn setFrame:CGRectMake(0,258,100,50)]
(increase width to 100)
and you may need to change the title colour if your background is white
[btn setTitleColor:[UIColor blackColor] forState:
UIControlStateNormal];
you Can first add button view then add imageview on the button please see bellow code :
//Add first button
UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame = CGRectMake(0, 4, 60, 56);
btnImage.backgroundColor = [UIColor colorWithRed:231.0f/255.0f green:232.0f/255.0f blue:234.0f/255.0f alpha:1.0];
btnImage.layer.borderColor = [UIColor colorWithRed:183.0f/255.0f green:184.0f/255.0f blue:186.0f/255.0f alpha:1.0].CGColor;
btnImage.layer.borderWidth = 1.0f;
// Add imageview on button
UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[yourImageView setImage:[UIImage imageNamed:#"myimage.png"]];
yourImageView.center = CGPointMake(btnImage.frame.size.width/2, btnImage.frame.size.height/2);
[btnImage addSubview:yourImageView];
[self.view addSubview:btnImage];
I'm trying to make an app for a restaurant and the to display the food menu as a scrollable image. The image is pulled form a config file.
- (void)viewDidLoad
{
[super viewDidLoad];
// back button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"btn-back.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 0.0f, 46.0f, 28.0f);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
[self setTitle:[menu objectForKey:#"title"]];
[scrollView2 setScrollEnabled:YES];
NSString *imageName = [menu objectForKey:#"imageName"];
if (![imageName isEqualToString:#""]) {
UIImage *image = [UIImage imageNamed:imageName];
[imageView setImage:image];
[scrollView2 addSubview:imageView];
[imageView release];
}
[textView setText:[menu objectForKey:#"description"]];
}
As of now the image doesn't scroll, but appears as a static image. Could anyone please help me modify this code so the user can vertically scroll the image.
Thanks a lot.
You should use a UIScrollViewand add the UIImageView as subview of the contentView.
And then set the contentsize of the scrollview as the imageviews size.
[imageView setImage:image];
[scrollView2.contentView addSubview:imageView];
[scrollView2 setContentSize:image.frame.size];
[imageView release];
have no Mac to test at the moment but that should work.
I agree mostly with what Seega wrote. Your imageView must be subview of a scrollView. Then, don't forget to set the scrollView's contentSize & the minimum&maximumZoomScale & and the imageView's frame:
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
self.scrollView.minimumZoomScale = // some number between 0 and 1, default is 1, so no zoom
self.scrollView.maximumZoomScale = // some number larger than 1, default is 1, so no zoom
self.scrollView.zoomScale = 1 // starting zoom scale
A nice place to put this code is in
-(void)viewWillLayoutSubviews{}
Thus it will be executed everytime the view appears & also whenever the device is rotated.