I use a UITableViewController to display a list of items, and one of the items selection must lead to a
photo gallery created with animationImages. I usually declare the controllers associated to
the first level selection with something like :
MyController *myController = [[MyController alloc] initWithStyle:UITableViewStylePlain];
myController.title = #"title 1";
myController.rowImage = [UIImage imageNamed:#"myControllerIcon.png"];
..
But I believe a UIViewController is needed to use animationImages..
How can I display a image animation directly from a table view ?
If you have you image sequence build in this manner:
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImage1.png"],
[UIImage imageNamed:#"myImage2.png"],
[UIImage imageNamed:#"myImage3.png"],
[UIImage imageNamed:#"myImage4.gif"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];
Snatched from here
Then you can build your own cell or add the animationImage to the cell by adding it to either:
[cell setImage:myAnimatedView];
[cell setBackgroundView:myAnimatedView];
[cell setSelectedBackgroundView:myAnimatedView]
Related
I have set up two barbutton items one on the right and one on the left. The right one successfully changes between two views, and the second one ont the right is a segmented control which is supposed to change between two views like wise. Here is my code, i want to know how to implement changing views with the segmented control.
- (void)setupNavigationBar {
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"ContainerView"];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(switchViewControllers)];
navBar.items = [NSArray arrayWithObject:navItem];
segmented = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"seg1.png"],
[UIImage imageNamed:#"seg1.png"],
nil]];
segmented.frame = CGRectMake(0, 0, 50, 30);
segmented.segmentedControlStyle = UISegmentedControlStyleBar;
UIBarButtonItem * segmentedBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmented];
navItem.leftBarButtonItem = segmentedBarItem;
[self.view addSubview:navBar];
}
- (void)viewDidLoad {
[super viewDidLoad];
// This is just for the purpose of calling the switch method.
[self setupNavigationBar];
CGRect rect = CGRectMake(0, 44, 320, 436);
currentViewController = [[FirstViewController alloc] init];
currentViewController.view.frame = rect;
nextViewController = [[SecondViewController alloc] init];
nextViewController.view.frame = rect;
[self addChildViewController:currentViewController];
[self addChildViewController:nextViewController];
[self.view addSubview:currentViewController.view];
}
- (void)switchViewControllers {
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:3.0 options:UIViewAnimationTransitionFlipFromRight | UIViewAnimationOptionCurveEaseInOut animations:nil completion:^(BOOL finished) {
UIViewController *tempViewController = currentViewController;
currentViewController = nextViewController;
nextViewController = tempViewController;
tempViewController = nil;
}];
}
How would I implement the segmented control to change views?
For add UISegmentedControl on UINavigationBar
EDIT :
NSArray *itemArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
nil];
UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc] initWithItems:itemArray];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
[menuSegmentedButton addTarget:self action:#selector(doMenu)
forControlEvents:UIControlEventValueChanged];
[menuSegmentedButton sizeToFit];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc]
initWithCustomView:menuSegmentedButton];
self.navigationItem.leftBarButtonItem = barButton;
Add this method for segment button tapped
-(void)doMenu
{
/// code for segment button tapped
}
I guess performance issues may require having separate view controllers rather than two subviews but it is much more complex - You would have a very complicated navigation system and would need to set the segment control for each view. I am sure there is another way to subclass a view but I am unfamiliar with this so would suggest having one view controller containing your views.
Note: If you were to programmatically create all the objects in each view, you could achieve great performance by removing objects from any hidden views.
So first you have a UISegmentedControl (as created in iPatel's answer). But add in this line [ctrl addTarget:self action:#selector(switchViewControllers) forControlEvents:UIControlEventValueChanged];
Then in switchViewControllers put:
if (ctrl.selectedSegmentIndex == 0) {
NSLog(#"One");
self.viewOne.hidden = NO;
self.viewTwo.hidden = YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"showView" forKey:#"viewOne"];
[defaults synchronize];
}
else if (ctrl.selectedSegmentIndex == 1) {
NSLog(#"Two");
}
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:#"showView"] isEqualToString:#"viewOne"]) {
self.viewOne.hidden = NO;
self.viewTwo.hidden = YES;
}
else if (...) {
}
}
The above should work when you toggle the segment control. In each if you can toggle the visibility of two views. You would create the different views as a subview of the main view; one on top of the other. You would need to hide one view either in Interface Builder or in the viewDidLoad (I would suggest storing the current view in NSUserDefaults and showing that view when the user first loads the view controller).
I've following code running on iPad, I'm usin Zbar 1.2.2 beta version for support in iPad.
ZBarReaderViewController* mReader = [[ZBarReaderViewController alloc] init];
mReader.readerDelegate = self;
mReader.showsZBarControls = NO;
mReader.wantsFullScreenLayout = NO;
mReader.readerView.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
UIButton *imageView = [[UIButton alloc] init];
imageView.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
[self setupToolBar:imageView];
mReader.cameraOverlayView = imageView;
setupToolBar does following
- (void)setupToolBar:(UIButton*)imageView
{
[imageView retain];
UIToolbar* imagePickerToolBar = [[UIToolbar alloc] init];
UIImage *c = [UIImage imageNamed:#"close.png"];
UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithImage:c style:UIBarButtonItemStylePlain target:self action:#selector(closeBarItemPressed:)];
NSArray *items = [NSArray arrayWithObjects: closeItem, nil];
[imagePickerToolBar setItems:items animated:NO];
imagePickerToolBar.frame = CGRectMake(0, UIScreen.mainScreen.bounds.size.height - imagePickerToolBar.frame.size.height, imagePickerToolBar.frame.size.width, imagePickerToolBar.frame.size.height);
[imageView addSubview:imagePickerToolBar];
[imageView bringSubviewToFront:imagePickerToolBar];
[imagePickerToolBar release];
[closeItem release];
[imageView release];
}
Now my question : closeBarItemPressed: is not getting called, why and how to fix it ?
I had this problem too. It looks like touches are ignored in the bottom part of the overlay view. Try moving the button a little bit higher and it will work. (in your case if you change toolbar frame to be in the top of the screen, I bet your method will be called, at least this is what happened to me). Not sure why this is happening though.
Basically I solved my problem, by recompiling Zbar, with modified iPad settings. Now it works OK.
I am trying to implement an animation set of images on my application.
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImage1.png"],
[UIImage imageNamed:#"myImage2.png"],
[UIImage imageNamed:#"myImage3.png"],
[UIImage imageNamed:#"myImage4.gif"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];
Ok, but the problem is that i am using an API function to place a marker on a map.
That function is the following:
RMMarker *newMarker;
UIImage *blueMarkerImage = [UIImage imageNamed:#"marker.png"];
newMarker = [[RMMarker alloc] initWithUIImage:blueMarkerImage anchorPoint:CGPointMake(0.5, 1.0)];
[mapView.contents.markerManager addMarker:newMarker AtLatLong:position];
[newMarker release];
The problem is that "initWithUIImage:" only works with UIImage, not with UIImageView.
So, how can i solve it??
UPDATE: My new code that is not working:
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"marker0.png"],
[UIImage imageNamed:#"marker1.png"],
[UIImage imageNamed:#"marker2.png"],
[UIImage imageNamed:#"marker3.png"],
[UIImage imageNamed:#"marker4.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 10; // seconds
myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
[myAnimatedView startAnimating];
RMMarker *newMarker = [[RMMarker alloc] initWithUIImage:[myAnimatedView image] anchorPoint:CGPointMake(0.5, 1.0)];
[mapView.contents.markerManager addMarker:newMarker AtLatLong:markerPosition];
You can try using image message from UIImageView:
newMarker =
[
[RMMarker alloc]
initWithUIImage:[myAnimatedView image]
anchorPoint:CGPointMake(0.5, 1.0)
];
Or, if what you need is creating a UIImageView then you can try:
UIImageView *newMakerImageView =
[[UIImageView alloc] initWithImage:
[
[RMMarker alloc]
initWithUIImage:yourImageHere
anchorPoint:CGPointMake(0.5, 1.0)
]
];
UIImageView *myAnimatedView = [UIImageView alloc]
That may just be a copy/paste error, but you forget the init.
UIImageView *myAnimatedView = [[[UIImageView alloc] initWithFrame:[self bounds]] autorelease]
I have an odd question here. I have two animations set up for a simple page flip. I then have a button that randomly generates a 1 or a 0. If the number is 0 then it performs one animation, if its 1 it performs another.
The issues I'm seeing is that this all runs fine on the simulator, but on the device it doesn't perform the animation if the random number is 0.
Any clues?
-(IBAction)pageTurn:(id)sender {
int randomNumber = arc4random() %2;
NSLog(#"randomNumber = %d", randomNumber);
if (randomNumber == 0) {
[self turnPageForward];
} else {
[self turnPageBackward];
}
}
-(void)turnPageForward {
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"PageTurnOne.png"],
[UIImage imageNamed:#"PageTurnTwo.png"],
[UIImage imageNamed:#"PageTurnThree.png"],
[UIImage imageNamed:#"PageTurnFour.png"],
[UIImage imageNamed:#"PageTurnFive.png"],
[UIImage imageNamed:#"PageTurnSix.png"],
[UIImage imageNamed:#"PageTurnSeven.png"],
[UIImage imageNamed:#"PageTurnEight.png"],
[UIImage imageNamed:#"PageTurnNine.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 480)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = .5; // seconds
myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
[myAnimatedView startAnimating];
[self.view addSubview:myAnimatedView];
[myAnimatedView release];
}
-(void)turnPageBackward {
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"PageTurnNine.png"],
[UIImage imageNamed:#"PageTurnEight.png"],
[UIImage imageNamed:#"PageTurnSeven.png"],
[UIImage imageNamed:#"PageTurnSix.png"],
[UIImage imageNamed:#"PageTurnFive.png"],
[UIImage imageNamed:#"PageTurnFour.png"],
[UIImage imageNamed:#"PageTurnThree.png"],
[UIImage imageNamed:#"PageTurnTwo.png"],
[UIImage imageNamed:#"PageTurnOne.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 480)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = .5; // seconds
myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
[myAnimatedView startAnimating];
[self.view addSubview:myAnimatedView];
[myAnimatedView release];
}
Usually, when code fails on the device but not the simulator (or vice versa) the culprit is a library or other compiled source that was compiled for one hardware but not the other.
Another related possibility is that a resource was not added properly so that it is not included in the final build for the device. I would check the image files in the method that does not work.
You should also confirm if the turnPageForward is being called at all. That will give you a clue as to where the problem lays.
This is the strangest error i have come across.
Am trying to add a countdown animation onto a Class that extends UIView. The code is shown below:
NSArray *myImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:#"3.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"Go.png"], nil];
UIImageView *myAnimatedView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 3;
myAnimatedView.animationRepeatCount = 0;
[self addSubview:myAnimatedView];
[myAnimatedView startAnimating];
It is just not appearing on the screen. Any Ideas?
Have you made sure those images are added to your project?
Can you add them as a flat image (non-animated) -- as in:
UIImageView *myAnimatedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"3.png"]];
[self addSubview:myAnimatedView];
(just to make sure that works first)?
If, instead of extending UIView, you extended UIImageView, would that make a difference?
Try switching the last 2 lines. From:
[self addSubview:myAnimatedView];
[myAnimatedView startAnimating];
To:
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];