How to trigger UIControlEventValueChanged of UISlider without touch UISlider in iOS? - ios

I have create an UISlider like the following code:
BrightnessSlider = [[UISlider alloc] initWithFrame:CGRectMake(sliderXpoint, 530, sliderwidth, 20)];
BrightnessSlider.minimumValue = 0;
BrightnessSlider.maximumValue = 100;
BrightnessSlider.value = 50;
BrightnessSlider.continuous = YES;
[BrightnessSlider addTarget:self action:#selector(BrightnessSliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[BrightnessSlider addTarget:self action:#selector(BrightnessSliderTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
And it will trigger the following event when I touch the UISlider.
- (void)BrightnessSliderValueChanged:(UISlider *)sender {
BrightnessLabel.text = [NSString stringWithFormat:#"%#%.0f", NSLocalizedString(#"Brightness", #""), sender.value];
}
I want to trigger the UIControlEventValueChanged without touch the UISlider.
For example , When I set the value to UISlider like the following code:
[BrightnessSlider setValue:(BrightnessSlider.value + 1)];
How do I trigger the UIControlEventValueChanged of UISlider without touch UISlider in iOS?

You can do something like this to trigger action programmatically:
[BrightnessSlider sendActionsForControlEvents:UIControlEventValueChanged];

You can fire
dispatch_async(dispatch_get_main_queue(), ^{
[BrightnessSlider sendActionsForControlEvents: UIControlEventTouchUpInside];
[BrightnessSlider sendActionsForControlEvents: UIControlEventTouchUpInside];
});
The key is make sure you do in main thread.

Related

How can I make UIButton disable which is created programmatically

I created some buttons in my code. I want to set it disable but I'm not sure how. here's my code.
UIButton *btn_levels = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_levels.frame=CGRectMake(x-10, y, 40, 40);
[btn_levels setTitle:[beginer_lvl objectAtIndex:i] forState:UIControlStateNormal];
[btn_levels addTarget:self action:#selector(btn_Method) forControlEvents:UIControlEventTouchUpInside];
btn_levels.tag =i;
btn_levels.backgroundColor=[UIColor blackColor];
btn_levels.tintColor=[UIColor cyanColor];
NSLog(#"btn nm=%#",[beginer_lvl objectAtIndex:i]);
[self.scroll addSubview:btn_levels];
and that is the button method but I don't know what to do...
-(void)btn_Method
{
//to make button disable
}
Modify this line
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
and this method
-(void)btn_Method:(UIButton*)sender
{
sender.enabled = NO;
}
change the statement
[btn_levels addTarget:self action:#selector(btn_Method) forControlEvents:UIControlEventTouchUpInside];
to
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
Also change the below method -
-(void)btn_Method
{
//to make button disable
}
to
-(void)btn_Method:(UIButton*)button
{
//to make button disable
[button setEnabled:NO];
}
disabled but visible
btn_levels.enabled = NO;
also invisible
btn_levels.hidden = YES;
Target actions can send the object for which it'll performed the action. To make your method to know which button was tap, you need to make its definition to get the button inside the method. So just change you button definition to look like this,
- (void) btn_Method:(UIButton *)sender {...}
also, where you're adding target to the button you've to add a colon ( : ) after the method name to tell the compiler that, this action will need an object on which an action was added. So that line should look like this,
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
^
Here, we will get a sender (you can give any name to your argument) of type UIButton with btn_Method action call. That's it, now you can do anything with that button, so in your case you want to make it disable, so the method would look like this,
- (void) btn_Method:(UIButton *)sender {
sender.enabled = NO;
}

Two method for UIButton?

We need to set a UIButton with 2 methods, the first one is when you touch it (down and up) you get one action , but when you long press it, you get another one.
For example, to get data about it when you long click, and when regular click , another thing.
How can i achieve this with UIButton ?
UIButton *CAT = [UIButton buttonWithType:UIButtonTypeCustom];
CAT.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
CAT.backgroundColor=[UIColor clearColor];
[CAT addTarget:self action:#selector(newcat:)forControlEvents:UIControlEventTouchUpInside];
I have started with adding it a gesture with this
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[CAT addGestureRecognizer:longPress];
But this triggered only when you take off your finger .
i want it to be triggered while my finger is still there after 1-2 seconds .
Can i do that? and can i adjust the time required for it to trigger ?
You can use the event UIControlEventTouchDown, use delay time for long press and you must deal with UIControlEventTouchUpInside and UIControlEventTouchUpOutside. good luck!
this is normal method
UIButton *CAT = [UIButton buttonWithType:UIButtonTypeCustom];
CAT.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
CAT.backgroundColor=[UIColor clearColor];
[CAT addTarget:self action:#selector(newcat:)forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *longpressGesture =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:#selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 5; // set the time interval
[longpressGesture setDelegate:self];
[CAT addGestureRecognizer:longpressGesture];
[self.view addsubview: CAT];
longpress action method
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(#"longPressHandler");
// do long press action
}
normal action method
-(void) newcat:(id)sender
{
// do normal action here
}

How to preform an action while the button is pressed?

I have a button and i need to change the label while the button is pressed to #"PRESSED" and if it's let go I need to change it to #"LET GO".
I've found many answers, but i have no idea how to implement them in Xcode 5.
That are the ones i tried:
You can start your action on the touchDownInside and stop it on the touchUpInside actions - you can hook them up in IB.
or
Add targets to your button for both control states, UIControlEventTouchDown and UIControlEventTouchUpInside or UIControlEventTouchUpOutside
or even
Add a dispatch source iVar to your controller...
dispatch_source_t _timer;
Then, in your touchDown action, create the timer that fires every so many seconds. You will do your repeating work in there.
If all your work happens in UI, then set queue to be
dispatch_queue_t queue = dispatch_get_main_queue();
and then the timer will run on the main thread.
- (IBAction)touchDown:(id)sender {
if (!_timer) {
dispatch_queue_t queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
// This is the number of seconds between each firing of the timer
float timeoutInSeconds = 0.25;
dispatch_source_set_timer(
_timer,
dispatch_time(DISPATCH_TIME_NOW, timeoutInSeconds * NSEC_PER_SEC),
timeoutInSeconds * NSEC_PER_SEC,
0.10 * NSEC_PER_SEC);
dispatch_source_set_event_handler(_timer, ^{
// ***** LOOK HERE *****
// This block will execute every time the timer fires.
// Do any non-UI related work here so as not to block the main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Do UI work on main thread
NSLog(#"Look, Mom, I'm doing some work");
});
});
}
dispatch_resume(_timer);
}
Now, make sure to register for both touch-up-inside and touch-up-outside
- (IBAction)touchUp:(id)sender {
if (_timer) {
dispatch_suspend(_timer);
}
}
Make sure you destroy the timer
- (void)dealloc {
if (_timer) {
dispatch_source_cancel(_timer);
dispatch_release(_timer);
_timer = NULL;
}
}
I am new to iOS and have no idea whatsoever how to do any of that! Thanks a lot for any help in advance!
I would do this with a GestureRecognzier. Here is some code:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(buttonIsPressed:)];
gr.minimumPressDuration = 0.1;
[self.button addGestureRecognizer:gr];
}
-(IBAction)buttonIsPressed:(UILongPressGestureRecognizer*)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
self.label.text = #"Button is pressed!";
}
else if (gesture.state == UIGestureRecognizerStateEnded) {
self.label.text = #"Button is not pressed";
}
}
You need an IBOutlet for the label and the button.
The first method does what you want, here's an example implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2.0 - 50.0, self.view.bounds.size.height / 2.0 - 25.0, 100.0, 50.0)];
myButton.backgroundColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.8 alpha:1.0];
[myButton setTitle:#"PRESS ME" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(setPressed:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:#selector(setLetGo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}
-(void)setPressed:(id)sender
{
UIButton *myButton = (UIButton *)sender;
[myButton setTitle:#"PRESSED" forState:UIControlStateNormal];
}
-(void)setLetGo:(id)sender
{
UIButton *myButton = (UIButton *)sender;
[myButton setTitle:#"LET GO" forState:UIControlStateNormal];
}
If you are new to xCode this can be difficult to understand. Follow this and you'll be allright.
Go to Storyboard and put a button in the view now go to the Inspector and change State config from Default to Highlighted
In placeholder 'highlighted title' put 'Pressed'
Now click on the Assistant Editor to open the .m file next to Storyboard
CNTRL + Click and drag from the Button to the .m file code to create a TouchUpInside event, mine called buttonPressed
Put this code in the method body and you are good to go!
This is what it will look like, pressed and released:

EXC_BAD_ACCESS on selector in objective C

I've got a UIBarButtonItem category where I build UIBarButtonItems with custom UIButtons, since I've found UIButtons easier to customize then UIBarButtonItems.
Now, I'd like to continue to use the BarButtonItem's target and action properties instead of using those in the button so that the BarButtonItem can continue to be customized externally without anyone having to know the implementation details (i.e., that it is using a button internally).
Now, in order to do that, I'm written up this code in my category:
+ (UIBarButtonItem *)backBarButtonItemWithColor:(UIColor *)color
{
UIImage *closeIcon = [MyImageUtility navBarBackArrow];
if (color) closeIcon = [closeIcon imageWithColorOverlay:color];
UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, closeIcon.size.width+10.0f, closeIcon.size.height+10.0f)];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:close];
[close setImage:closeIcon forState:UIControlStateNormal];
[close addTarget:item action:#selector(SD_executeBarButtonItemAction) forControlEvents:UIControlEventTouchUpInside];
return item;
}
- (void)SD_executeBarButtonItemAction
{
[self.target performSelector:self.action];
}
Whenever the SD_executeBarButtonItemAction is called, I get a exc_bad_access on the selector, though I am not sure why. Any ideas? Is there a way around this?
Thanks!
EDIT:
here is the code being called by that selector that is crashing:
void (^transition)(void) = ^(void) {
[self.rightContainer setFrame:[self offscreenContainerFrame]];
[self.centerContainer setAlpha:1.0f]; //TODO: this is unreliable in iOS6 -- we should add a view to the top of it to darken
[self.centerContainer setTransform:CGAffineTransformIdentity];
};
[self notifyWillShowPrimaryViewController];
[self performBlock:transition animated:YES completion:^(BOOL finished) {
[self notifyDidShowPrimaryViewController];
[self setForegroundController:self.primaryNavigationController];
if (block != NULL) block(finished);
}];
Your code is a recursive call.
- (void)SD_executeBarButtonItemAction
{
[self.target performSelector:self.action];
}
You set like:
[close addTarget:item action:#selector(SD_executeBarButtonItemAction) forControlEvents:UIControlEventTouchUpInside];
Where item is a UIBarButtonItem.

button of a dynamically created view does not respond

I need your help. I am trying to build a game. When a hint button is pressed a new view is created programmatically with the code below:
In my gameController.m
//connect the Hint button
-(void)setHud:(HUDView *)hud
{
_hud = hud;
[hud.btnHelp addTarget:self action:#selector(actionHint) forControlEvents:UIControlEventTouchUpInside];
}
//the user pressed the hint button
-(void)actionHint
{
CGRect viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight,kMenuWidth*10, kMenuHeight*2);
HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
[self.gameView addSubview:hintMenu];
}
This is my new View created programmatically.
+(instancetype)viewWithRect:(CGRect)r
{
HintMenu* hint = [[HintMenu alloc] initWithFrame:r];
hint.userInteractionEnabled = YES;
UIImage* image=[UIImage imageNamed:#"btn"];
hint.btnHelp = [UIButton buttonWithType:UIButtonTypeCustom];
[hint.btnHelp setTitle:#"button" forState:UIControlStateNormal];
hint.btnHelp.titleLabel.font = kFontHUD;
[hint.btnHelp setBackgroundImage:image forState:UIControlStateNormal];
hint.btnHelp.frame = CGRectMake(50, 30, image.size.width, image.size.height);
hint.btnHelp.alpha = 0.8;
[hint addSubview: hint.btnHelp];
UIImage* image2=[UIImage imageNamed:#"tile"];
hint.imageHelp=[[UIImageView alloc] initWithFrame:CGRectMake(image.size.width + 50 + 10, 30, image2.size.width, image2.size.height/2)];
hint.imageHelp.image=image2;
[hint addSubview:hint.imageHelp];
return hint;
}
I want to identify when the button in the new view is being pressed. Similarly with the above I add in my GameController.m
//connect the button in the Menu
-(void)setHintMenu:(HintMenu *)hintMenu
{
_hintMenu = hintMenu;
[hintMenu.btnHelp addTarget:self action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
}
//the user pressed the menu button
-(void)actionHintMenu
{
NSLog(#"Button in menu pressed");
}
but the log is not shown. Can you help me on this?
edit:
If this code is added in the HintMenu.m which is my new Created view like this:
+(instancetype)viewWithRect:(CGRect)r
{
...
hint.btnHelp.alpha = 0.8;
[hint.btnHelp addTarget:self action:#selector(actionHintMenu:) forControlEvents:UIControlEventTouchUpInside];
[hint addSubview: hint.btnHelp];
..
}
//the user pressed the menu button
-(void)actionHintMenu:(UIButton *) button
{
NSLog(#"Button in menu pressed");
}
I am getting this error:
2013-06-16 16:44:30.559 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[HintMenu actionHintMenu]: unrecognized selector sent to class 0xe6f8'
*** First throw call stack:
Answer to you edited question
Replace
[hint.btnHelp addTarget:self action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
with
[hint.btnHelp addTarget:hint action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
I am not sure why you have added [hintMenu.btnHelp addTarget:self action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside]; inside the setter method of your hintMenu view.
There is no call for the setter method of hintMenu and so the button target-action is not getting set.
I would suggest remove the -(void)setHintMenu:(HintMenu *)hintMenu completely.
Update actionHint
-(void)actionHint
{
CGRect viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight,kMenuWidth*10, kMenuHeight*2);
HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
[hintMenu.btnHelp addTarget:self action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
[self.gameView addSubview:hintMenu];
}
This is just a fix. If you ask me the right method, I would suggest setting the button target should be done within your custom view itself. For that you might maintain a delegate property in the view.

Resources