Hi,
i'm trying to animate the image of a UIButton between two different image.
Let's say i have two square images, on RED, on BLACK.
When i touch my Button, i want the image of the uibutton to loop from both images, in a smooth animation.
the loop animation must start whenever the user touch down the button and canceled when he touch Up inside or drag exit.
Here is what i do now :
-(void)startAnimation:(UIButton *)obj {
[UIView animateWithDuration:1 delay:0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
[obj setAlpha:0];
[obj setImage:[UIImage imageNamed:#"animated_on"] forState:UIControlStateNormal];
[obj setImage:[UIImage imageNamed:#"animated_on"] forState:UIControlStateHighlighted];
[obj setImage:[UIImage imageNamed:#"animated_on"] forState:UIControlStateDisabled];
[obj setImage:[UIImage imageNamed:#"animated_on"] forState:UIControlStateSelected];
} completion:^(BOOL finished) {
[obj setAlpha:1];
[obj setImage:nil forState:UIControlStateNormal];
[obj setImage:nil forState:UIControlStateHighlighted];
[obj setImage:nil forState:UIControlStateDisabled];
[obj setImage:nil forState:UIControlStateSelected];
}];
}
-(void)stopAnimation:(UIButton *)obj {
[obj.layer removeAllAnimations];
}
- (void)touchDownAnimation:(id)obj {
[self startAnimation:obj];
}
- (void)dragEnterAnimation:(id)obj {
[self startAnimation:obj];
}
- (void)dragExitAnimation:(id)obj {
[self stopAnimation:obj];
}
- (void)touchUpInsideAnimation:(id)obj {
[self stopAnimation:obj];
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(110, 180, 100, 100);
UIImageView *animatedButtonBackground = [[UIImageView alloc] initWithFrame:frame];
[animatedButtonBackground setImage:[UIImage imageNamed:#"animated_off"]];
[self.view addSubview:animatedButtonBackground];
UIButton *animatedButton = [UIButton buttonWithType:UIButtonTypeCustom];
[animatedButton setFrame:frame];
[animatedButton addTarget:self action:#selector(touchDownAnimation:) forControlEvents:UIControlEventTouchDown];
[animatedButton addTarget:self action:#selector(dragEnterAnimation:) forControlEvents:UIControlEventTouchDragEnter];
[animatedButton addTarget:self action:#selector(dragExitAnimation:) forControlEvents:UIControlEventTouchDragExit];
[animatedButton addTarget:self action:#selector(touchUpInsideAnimation:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:animatedButton];
}
it's kinda works, but involving an UIImageView in the process looks like a bad trick to do what i want...
i'm looking for a cleaner solution.
Wonder if you could use something simpler:
button.imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
nil];
button.imageView.animationDuration = 1;
[button.imageView startAnimating];
Related
Weird thing
navigationbar'alpha value changes after dismissing view controller
Step
Click button that presents a new viewcontroller
Set navigation bar alpha to 0.0 when the button is clicked
Dismiss viewcontroller which I presented right before.
Result [NG]
navigationbar'alpha value is set to 1.0
Expected result
navigationbar's alpha should be 0.0 as expected
Below sample is pretty much easy
ViewController class
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *clickMe = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
[clickMe setTitle:#"clickme" forState:UIControlStateNormal];
[clickMe setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickMe addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickMe];
}
- (void)btnClicked {
self.navigationController.navigationBar.alpha = 0.0f;
second *sec = [[second alloc] init];
[self presentViewController:sec animated:YES completion:nil];
}
second class
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
self.view.backgroundColor = [UIColor yellowColor];
UIButton *getBack = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
[getBack setTitle:#"getBack" forState:UIControlStateNormal];
[getBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[getBack addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:getBack];
}
- (void)btnClicked {
[self dismissViewControllerAnimated:YES completion:nil];
}
I really would like to know what happened in this..
If someone knows this, let me know.. Thank you in advance.
i'm a newbie in Objective-C can someone help me out here? I am trying to make a carousel of button but i failed to do it.
What i got it just displaying all the buttons on load and that's it. I can't add action on each button, I mean tagging them.
It's my first to do declare button in this way.
The buttons are not working.
Here's my code:
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
mainHolder = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"feat.png"]];
[mainHolder setUserInteractionEnabled:YES];
[mainHolder setFrame: CGRectMake(0, 0, 1024, 768)];
[self.view addSubview: mainHolder];
header = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header.png"]];
[header setFrame:CGRectMake(0, 0, 1024, 50)];
[header setUserInteractionEnabled:YES];
[mainHolder addSubview:header];
// The items to be displayed in the carousel
items = [NSArray arrayWithObjects:
[UIImage imageNamed:#"picOne.png"],
[UIImage imageNamed:#"picTwo.png"],
[UIImage imageNamed:#"picThree.png"],
[UIImage imageNamed:#"picFour.png"],
[UIImage imageNamed:#"picFive.png"],
[UIImage imageNamed:#"picSix.png"],
[UIImage imageNamed:#"picSeven.png"],
[UIImage imageNamed:#"picEight.png"],
[UIImage imageNamed:#"picNine.png"],
[UIImage imageNamed:#"picTen.png"],
[UIImage imageNamed:#"picEleven.png"],
nil];
// Initialize and configure the carousel
carousel = [[iCarousel alloc] initWithFrame:self.view.bounds];
carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
carousel.type = iCarouselTypeCoverFlow2;
carousel.dataSource = self;
[self.view addSubview:carousel];
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [items count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [items objectAtIndex:index];
imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease];
[imageChoices setBackgroundImage:image forState:UIControlStateNormal];
[imageChoices setUserInteractionEnabled:YES];
[imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50];
[imageChoices addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
imageChoices.tag=index;
return imageChoices;
}
- (void)buttonTapped:(UIButton *)sender
{
//I don't know how to tag each button here.
switch(sender.tag){
case 1:{
NSLog(#"Photo1");
}
break;
case 2:{
NSLog(#"Photo2");
}
case 3:{
NSLog(#"Photo3");
}
break;
case 4:{
NSLog(#"Photo4");
}
break;
case 5:{
NSLog(#"Photo5");
}
break;
}
}
Implement the target method this way:
- (IBAction)buttonTapped:(id)sender
{
switch(sender.tag){
case 1:{
NSLog(#"Photo1");
break;
}
case 2:{
NSLog(#"Photo2");
break;
}
case 3:{
NSLog(#"Photo3");
break;
}
case 4:{
NSLog(#"Photo4");
break;
}
case 5:{
NSLog(#"Photo5");
break;
}
default:{
NSLog(#"Default");
break;
}
}
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [items objectAtIndex:index];
//Instead of adding single UIButton, add separate for each carousel.
UIButton *imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease];
[imageChoices setBackgroundImage:image forState:UIControlStateNormal];
[imageChoices setUserInteractionEnabled:YES];
[imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50];
[imageChoices addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
imageChoices.tag=index; //print index, that it should be unique or not?
return imageChoices;
}
Try this
- (void)buttonTapped:(UIButton *)sender
{
UIButton *sendButton = (UIButton *)sender; //No need of this! We already have sender
switch(sendButton.tag)
{
//.......
}
}
add carousel.delegate = self; In your loadview method and then implement
- (void)carousel:(iCarousel *)crsl didSelectItemAtIndex:(NSInteger)index { }
Since starting with iPhone app development (last 9 months) I have only used IB. I have a project to work on already built by another developer that I need to optimise for iPhone screen. No problem in IB, I know how to do that, in this project however the Nav bar is added using code only and is an image view. Could someone advise me how I go about resizing/positioning the nav bar when IB isnt used? Im trying to enhance this app for the iphone 5 screen.
#define BAR_FRAME CGRectMake(0,0,320.0f,43.0f)
#implementation ICNavbarView
#synthesize homeButton=__homeButton;
#synthesize prevButton=__prevButton;
#synthesize nextButton=__nextButton;
#synthesize delegate=__delegate;
- (id)initWithFrame:(CGRect)frame
{
LogCmd();
self = [super initWithFrame:BAR_FRAME];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.alpha = 0.9f;
// Add Navigation bar background // <<<<<< navigation bar from ui image
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
// Add back button
__prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
__prevButton.frame = CGRectMake(30.0f, 6.0f, 29.0f, 31.0f);
UIImage *prevButtonPressed = [UIImage imageNamed:#"BtnPrevPressed"];
[__prevButton setImage:[UIImage imageNamed:#"BtnPrev"] forState:UIControlStateNormal];
[__prevButton setImage:prevButtonPressed forState:UIControlStateSelected];
[__prevButton setImage:prevButtonPressed forState:UIControlStateHighlighted];
[__prevButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__prevButton];
// Add next button
__nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
__nextButton.frame = CGRectMake(262.0f, 6.0f, 29.0f, 31.0f);
UIImage *nextButtonPressed = [UIImage imageNamed:#"BtnNextPressed"];
[__nextButton setImage:[UIImage imageNamed:#"BtnNext"] forState:UIControlStateNormal];
[__nextButton setImage:nextButtonPressed forState:UIControlStateSelected];
[__nextButton setImage:nextButtonPressed forState:UIControlStateHighlighted];
[__nextButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__nextButton];
// Add home button
__homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
__homeButton.frame = CGRectMake(145.0f, 6.0f, 31.0f, 30.0f);
UIImage *homeButtonPressed = [UIImage imageNamed:#"BtnHomePressed"];
[__homeButton setImage:[UIImage imageNamed:#"BtnHome"] forState:UIControlStateNormal];
[__homeButton setImage:homeButtonPressed forState:UIControlStateSelected];
[__homeButton setImage:homeButtonPressed forState:UIControlStateHighlighted];
[__homeButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__homeButton];
}
return self;
}
- (id)init
{
return [self initWithFrame:CGRectZero];
}
#pragma mark - Button handlers
- (void)buttonPressed:(id)sender
{
if (sender == __prevButton) {
[self.delegate performSelector:#selector(navBarPrevButtonPressed)];
} else if (sender == __homeButton) {
[self.delegate performSelector:#selector(navBarHomeButtonPressed)];
} else {
[self.delegate performSelector:#selector(navBarNextButtonPressed)];
}
}
#end
So far I tried UIViewAutoresizingFlexibleHeight; like this
// Add Navigation bar background
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
//resize
bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
This seems to make no difference and the nav bar hasn't moved
I've used this before, but I used the setter function instead of using the property. I don't know if that would change anything or not, but try it this way:
[bgView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
The parens are important, if you have multiple re-sizing options.
menuView= [[UIImageView alloc] initWithFrame:CGRectMake(0,411, 320,49)];
UIButton *test = [UIButton buttonWithType:UIButtonTypeCustom ];
test.frame = CGRectMake(0, 0, 80, 49);
[test addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[menuView addSubview:test];
[self.view addSubview:menuView];
I touch up inside (test ) button. but is not working . why??
Maybe you mixed buttonPressed: with buttonPressed.
If you implement a method like
- (void)buttonPressed
{
}
You need use
[test addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
Note: Pay attention on the colon.
But depending on what you have done, you need implement a method like
- (void)buttonPressed:(id)sender
{
}
for a cleaner solution you can try HMUIButtonOnTouch
source: https://github.com/HelloMihai/HMUIButtonOnTouch
[self.myButton onTouchInside:^{
NSLog(#"button tap");
}];
How to create a UIButton With two actions.
I know by using UILongPressGestureRecognizer we can perform Longpress.
But my requirement is,When I Long Press UIButton,it has to perform one action and when touch
up inside it, it has to perform another action.
Thanks.
Below is my code.
UIImage *redImage = [UIImage imageNamed:#"TabFav2.png"];
tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[tabRedbutton setImage:redImage forState:UIControlStateNormal];
tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35);
redTAb = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];
[tabRedbutton addTarget:self action:#selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside];
longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
longpressGesture1.minimumPressDuration =0.1;
[longpressGesture1 setDelegate:self];
longpressGesture1.cancelsTouchesInView = NO;
[tabRedbutton addGestureRecognizer:longpressGesture1];
[longpressGesture1 release];
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
if (longpressGesture.state == UIGestureRecognizerStateBegan)
{
NSlog(#"Long press");
}
}
-(void)redbottonmethod
{
NSlog(#"single tapped");
}
For the tap you can use UIButton's "addTarget:..." method and for the longpress you can add a gesture recognizer:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100.0, 100.0, 100.0, 20.0);
[btn setTitle:#"Test" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(userTapped:) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] init];
[gr addTarget:self action:#selector(userLongPressed:)];
[btn addGestureRecognizer:gr];
[gr release];
[self.view addSubview:btn];
Of course you need to implement the 2 methods that will be called:
- (void)userTapped:(id)sender {
NSLog(#"user tapped");
}
- (void)userLongPressed:(id)sender {
NSLog(#"user long pressed");
}
Hope that helps.
=========
EDIT: It seems that you are using your button as a BarButtonItem inside a UIToolbar. So I changed my code to do the same:
- (void)viewDidLoad {
[super viewDidLoad];
// set up the button
UIImage *redImage = [UIImage imageNamed:#"TabFav2.png"];
UIButton *tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom];
tabRedbutton.backgroundColor = [UIColor redColor];
[tabRedbutton setImage:redImage forState:UIControlStateNormal];
tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35);
// set up a bar button item with the button as its view
UIBarButtonItem *redTab = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];
// set up toolbar and add the button as a bar button item
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 100.0, 768.0, 40.0)];
toolbar.barStyle = UIBarStyleBlack;
NSArray *items = [NSArray arrayWithObject:redTab];
[toolbar setItems:items];
[self.view addSubview:toolbar];
[toolbar release];
// add tap handler to button for tap
[tabRedbutton addTarget:self action:#selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside];
// add gesture recognizer to button for longpress
UILongPressGestureRecognizer *longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
longpressGesture1.minimumPressDuration =0.1;
[tabRedbutton addGestureRecognizer:longpressGesture1];
[longpressGesture1 release];
}
And the two methods that get called:
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(#"Long press");
}
-(void)redbottonmethod {
NSLog(#"single tapped");
}
This code definitely works.
By the way: I noticed that in your code in the 2 methods that get called you have typo: You must use NSLog() and not NSlog(). Could that be the problem?