Animate iphone tableviewCell background image with two images - iphone

i want to animate tableviewcell background image with two blinking images on didSelectRowAtIndexPath. We can use following code, but it will show one image in background. However I want gif like image which blinks.
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"correctAnswer.png"]]autorelease];

UIImageView has the ability to do animation with the animationImages property.
UIImageView* imgView = [[[UIImageView alloc] init] autorelease];
imgView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"correctAnswer1.png"],
[UIImage imageNamed:#"correctAnswer2.png"], nil];
imgView.animationDuration = 0.5;
cell.backgroundView = imgView;
[imgView startAnimating];
You need to have each frame of the animation included in your project, correctAnswer1.png, correctAnswer2.png, etc...
Also, wouldn't it be better to use UITableViewCell's imageView property? That way you would get your animation in the left of the cell and it wouldn't interfere with the text or any other parts of the cell...

Related

How to reset the background of a UIView

After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think

Set background image in table view when story boarding is also used

I am doing the following to set the background image:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"appback_new.png"]];
[self.tableView setBackgroundView:imageView];
in view did load method. Also, I made sure that the background color is set to clear color in story board.
But when the view loads, it loads with a black color.
Did any one face this issue and knows how to resolve it?
Thanks.
Try this it should work :
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"appback_new.png"]];
self.tableView.backgroundColor = background;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"appback_new.png"]];
try your code after inserting this line of code
tableView.backgroundColor = [UIColor clearColor];

UIImageView animation is not displayed on view

In my Iphone App, I used the following code for the animation of those 3 images in viewDidLoad() method and I use left transform to load this ViewController. But these imageView is not displayed on the view.
NSArray *newArray = [[NSArray alloc]initWithObjects:[UIImage imageNamed:#"search1.png"],
[UIImage imageNamed:#"search2.png"],
[UIImage imageNamed:#"seach3.png"],
nil];
UIImageView *imageView = [UIImageView alloc];
[imageView initWithFrame:CGRectMake(240, 60, 60, 30)];
imageView.animationImages = newArray;
imageView.animationDuration = 0.25;
imageView.animationRepeatCount = 3;
[self.view addSubview:imageView];
Animation won't start until you tell it to start. You will need to add a command to startAnimating. I also cleaned up your alloc/init and I added a command to make the image view still keep showing when it is not animating. Setting an animation will not make the image appear. The image property is for the still image and the animationImages property is for the animating images of a UIImageView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 60, 60, 30)];
imageView.image = [UIImage imageNamed:#"search3.png"]; //This will be the image that is visible when it is NOT animating.
imageView.animationImages = newArray;
imageView.animationDuration = 0.25;
imageView.animationRepeatCount = 3;
[imageView startAnimating]; //This will make the animation start
[self.view addSubview:imageView];
Finally, your third image is missing an r. I think you want #"search.png" and not #"seach.png"

How to set UITableView background to image?

I've noticed that a number of apps have a custom background image for UITableView (or have set the background to a color.)
I can't find any API in the UITableView class to affect this, and my attempts in interface builder have failed (trying to set the color of the UIView)
Any suggestions? I've seen a third party app with pictures, so I know it can be done.
I just ran into the same question myself, but I found a way to do it that TomSwift touched on. As he mentioned, UITableView has a backgroundView property that is intended to do just what you're looking for. If you want to set the background color, you should instead use the backgroundColor property, as others have said. So:
// Set an image as the background of a UITableView called 'tableView'
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MyImage.png"]];
[tableView setBackgroundView:bg];
or:
// Set a solid color as the background of a UITableView called 'tableView'
// In this case I chose red
[tableView setBackgroundColor:[UIColor redColor]];
add this line in you code where you want to set background color for your image
[YourView(or imageView) setbackgroundColor:[UIColor
groupTableViewBackgroundColor]];
Thanks
UIImageView *image = [[UIImageView alloc] initWithImage:
[UIImage imagenamed:#"no_image.png"]];
[self.view addSubview:image];
UITableView *tableview = [[UITableView alloc] init];
[image addSubview:tableview];
tableview.backgroundcolor = [UIColor clearColor];
try this. it works for me.
CALayer *background = [CALayer layer];
background.zPosition = -1;
background.frame = self.view.frame;
background.contents = [[UIImage imageNamed:#"background.jpg"] CGImage];
[tableView.layer addSublayer:background];
you need to add an image view and set the table background color to clear color see this link for tutorial
Set the backgroundView property of the tableView. You can set it to a view where you have set a custom backgroundColor, or you can set it to a UIImageView where you've set an image.
alternative solution of GhostRider
No coding
Make UiImageView by the interface builder,
put your image in resources.
set it in UIImageView Image property by the use of Inspector.
select table view and set it background color to clear(for this make opacity to 0 by using attribute inspector).
This is how I did it. Probably not the best way, but works well enough.
UIImage *bg = [UIImage imageNamed:#"bg"];
[bg drawInRect:self.view.window.frame];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:self.view.window.frame];
bgView.image = bg;
activityTable.backgroundView = bgView; //activityTable = UITableView

UITableViewCell background image and selection problem

It's my first iphone app, and i have trouble with styling my tableView.
I have two images (png), one for standard cell state, and one for selected state.
In my subclassed cell, I tried the following :
1) setting up front the backgroundView and selectedBackgroundView
UIImage *ib = [UIImage imageNamed:#"tab.png"];
UIImageView *back = [[UIImageView alloc] initWithImage:ib];
self.backgroundView = back;
[back release];
UIImage *is = [UIImage imageNamed:#"selected_tab.png"];
UIImageView *selected = [[UIImageView alloc] initWithImage:is];
self.selectedBackgroundView = selected;
[selected release];
The standard cell is fine, but when selected, the two images are shown.
2) just playing with background view on selection :
// storing the 2 uiviews in class attributes
UIImage *ib = [UIImage imageNamed:#"tab.png"];
UIImageView *back = [[UIImageView alloc] initWithImage:ib];
self.storedStandard = back;
[back release];
UIImage *is = [UIImage imageNamed:#"selected_tab.png"];
UIImageView *selected = [[UIImageView alloc] initWithImage:is];
self.storedSelected = selected;
[selected release];
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.backgroundView = self.storedSelected;
}
else {
self.backgroundView = self.storedStandard;
}
}
It nearly works, but the images don't fit the cell, i don't know to stretch them to the size of the cell.
For me, the first solution should have been the one, based on the properties' names, and the second solution seems like a hack (like 90% of the tutorials i seen btw), so i'm a bit frustrated.
To sum up : why the first one doesn't work, and how could i force images to take all the cell space ?
Thanks a lot :)
if you have a UITableViewDelegate, you should be able to just change the backgroundView's image in the willSelectRowAtIndexPath: method and then change it back in the didSelectRowAtIndexPath: method. This is a bit of a hack however.