scroll view + segment button +iOS - ios

I have 2 scroll views. I put many buttons onto the first scroll view. When I click one button in the first scroll view, related buttons fill the second scroll view.
I want the buttons in the first scroll view to have the effect as if they are segment control. i.e. when one button is clicked, its image becomes dim and can't be clicked again until other buttons in the first scroll vew are clicked.
How to achieve this? any sample code is appreciated! thanks!

Can't you just keep track of which button was last clicked, and reset that one to be in the active state, and set the one you're now clicking to the inactive state? Just create a property, lastClicked, and do this:
-(IBAction) buttonClicked:(UIButton *)sender {
[self.lastClicked setUserInteractionEnabled:YES];
[self.lastClicked setImage:[UIImage imageNamed:#"enabledImage"] forState:UIControlStateNormal];
[sender setUserInteractionEnabled:NO];
[sender setImage:[UIImage imageNamed:#"disabledImage"] forState:UIControlStateNormal];
self.lastClicked = sender;
}

Try adding a segmented control inside a scroll view.
- (void)viewDidLoad
{
[super viewDidLoad];
journals = [[NSMutableArray alloc]init];
self.tableView.dataSource = self;
self.tableView.delegate = self;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 49, 320, 29)];
self.segmentedControl.frame = CGRectMake(0, 0, 640, 29);
scrollView.contentSize = CGSizeMake(self.segmentedControl.frame.size.width, self.segmentedControl.frame.size.height -1);
scrollView.showsHorizontalScrollIndicator = NO;
self.segmentedControl.selectedSegmentIndex = 0;
[scrollView addSubview:self.segmentedControl];
[self.view addSubview:scrollView];
[self fillJournals];
// Do any additional setup after loading the view, typically from a nib.
}

Super simple, very ugly, but it works. You could optimize this out further by having it check the image and the interaction state of the button, so you aren't setting an image every time this loops through.
-(void) buttonClicked:(UIButton *)sender {
for (UIButton *btn in self.scrollView.subviews) {
if ([sender isKindOfClass:[UIButton class]]) {
if (btn == sender) {
[btn setUserInteractionEnabled:NO];
[btn setImage:[UIImage imageNamed:#"disabledImage"] forState:UIControlStateNormal];
} else {
[btn setUserInteractionEnabled:YES];
[btn setImage:[UIImage imageNamed:#"enabledImage"] forState:UIControlStateNormal];
}
}
}
}

Related

UIButton in a subview not response to touch events with objective-c

I want to create a bottom tab that could change the view when user press.
To do that I create a view with view controller and sets it's frame in the init
Here is the bottom panel view controller
#implementation BottomTabViewController
-(id) initWithFrame:(CGRect)frame{
if(self = [super init]){
self.view.frame = frame;
return self;
}else{
return nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpButtons];
// Do any additional setup after loading the view.
}
-(void) featureBtnClick:(id*) sender{
NSLog(#"featureBtn Clicked");
}
-(void) favBtnClick:(id*)sender{
NSLog(#"Fav Btn Clicked");
}
-(void) setUpButtons{
UIButton *features = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
[features setBackgroundImage:[UIImage imageNamed:#"feature.png"] forState:UIControlStateNormal];
features.backgroundColor = [UIColor greenColor];
[features addTarget:self action:#selector(featureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:features];
UIButton *favBtn = [[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen]bounds].size.width - 100, 10, 50, 50)];
[favBtn setBackgroundImage:[UIImage imageNamed:#"favorite.png"] forState:UIControlStateNormal];
[favBtn addTarget:self action:#selector(favBtnClick:) forControlEvents:UIControlEventTouchUpInside];
favBtn.backgroundColor = [UIColor greenColor];
[self.view addSubview:favBtn];
}
#end
And I added that to my view with code like this:
-(void) setUpBottom{
BottomTabViewController *botm = [[BottomTabViewController alloc]initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 55, [[UIScreen mainScreen]bounds].size.width, 55)];
botm.view.backgroundColor = [UIColor redColor];
// botm.view.userInteractionEnabled = false;
botm.view.userInteractionEnabled = true;
[self.view addSubview:botm.view];
}
Here here is the result seems, note the bottom panel that works, well pretty silly, but it shows up:
But when press the left and right button, there is not log printed which it expected to be to indict that the button works, What have I done wrong? Thanks
you are adding buttons in the view first then adding the bottom view.
if this is what you are doing then it can be overlapped by the bottom view.
In this case buttons cant be tapped because they have bottom view on top of them.
so make sure u add bottom view first then your buttons. (or change their frames to avoid overlapping)

show and remove same button in iOS

I have one UIButton. Now I add button on Subview :-
UIButton* button4 = [[UIButton alloc] initWithFrame:frame4];
[button4 setBackgroundImage:image3 forState:UIControlStateNormal];
[button4 setShowsTouchWhenHighlighted:YES];
[button4 addTarget:self action:#selector(Nofication:) forControlEvents:UIControlEventTouchDown];
- (void)Nofication:(id)sender
{
share=[[UIView alloc]init];
share.frame=CGRectMake(300, 60, 130, 100);
[self.view addSubview:share];
[UIView animateWithDuration:2.0
animations:^{
share.frame =CGRectMake(180, 50, 130, 100); // its final location
}];
share.backgroundColor=[UIColor yellowColor];
login=[[UIButton alloc]init];
login.frame=CGRectMake(0, 0, 130, 50);
[login setBackgroundImage:[UIImage imageNamed:#"login.png"] forState:UIControlStateNormal];
[login addTarget:self action:#selector(asubmit:) forControlEvents:UIControlEventTouchDown];
[share addSubview:login];
login.layer.borderColor = [[UIColor whiteColor]CGColor];
policies=[[UIButton alloc]init];
policies.frame=CGRectMake(0, 50, 130, 50);
[policies setBackgroundImage:[UIImage imageNamed:#"Policies.png"] forState:UIControlStateNormal];
[policies addTarget:self action:#selector(apolicies:) forControlEvents:UIControlEventTouchDown];
[share addSubview:policies];
}
When I click button it's open subview working nice. But now I need if subview is showing in that time click button it's need to hidden subview. If subview is not showing in that time when I click button it's need to show subview. So Please give me any idea about it.
How to Show and Hidden same UIButton
thanks in Advance .
There is a hidden property on UIView. https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instp/UIView/hidden
if (button.isHidden) {
button.hidden = NO;
}
Is it what asked?
Where subview is the view (or UIButton) you are trying to hide if shown, and show if hidden.
[subview setHidden:![subview isHidden]];
Edit :
In your Notification function :
// Be sure that the view is loaded, maybe load it at a different place or use a boolean
if([subview isHidden]){
[subview setHidden:false];
}else{
[subview setHidden:true];
}
You can set button4.selected = !button4.selected in Nofication: method.
Then you can check button4.isSelected and hide/show subview with hidden property
Hope it helps.

How to set subview as a first responder?

I am trying to close the image View while clicking on close button.but button does not responds to touch event.
here is my code:
-(void)imageTouchUP:(id)sender
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.image=self.testimonialImage;
imageView.tag=1;
UIButton *closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame=CGRectMake(0, 0, 20, 20);
[closeButton setImage:[UIImage imageNamed:#"close.jpg"] forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:closeButton];
[self.view addSubview:imageView];
}
-(void)buttonClicked:(id)sender
{
UIView *subView=[self.view viewWithTag:1];
[subView removeFromSuperview];
}
For the UIImageView to handle touches you need to set userInteractionEnabled to YES, it defaults to NO.
Try:
[imageView setUserInteractionEnabled:YES];
Reference
There is 1 property of UIImageView userInteractionEnabled and its value is by default NO. So in your code add
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.userInteractionEnabled = YES;
Maybe there's another view with tag=1 in your self.view. Please try to edit button clicked like this:
- (void) buttonClicked:(UIButton *)btn
{
UIView *view = btn.superview;
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}

Showing custom UIView when row selected

I want to know if it possible and how to implement following user action:
The user taps on a button that is shown as UIView on a tableView row. After tapping on the button, a UIView containing buttons should appear, like in the image below:
UPDATED QUESTION:
I have changed my requirements. The UIView should be shown after swipping the row from left to right.
I have included following code to my cellForRowAtIndexPath method:
//swipe left-right
UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGestureLeftRight:)];
[swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
[cell addGestureRecognizer:swipeLeftRight];
//end of swipe left-right
And I have added the handleGestureLeftRight method:
-(void)handleGestureLeftRight:(UIGestureRecognizer *)gec
{
{
CGPoint p = [gec locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if (gec.state == UIGestureRecognizerStateEnded) {
NSLog(#"GESTURE LEFT-->RIGHT DONE");
CGRect newSize = CGRectMake(0.0f ,0.0f, 150.f, 50.0f);
UIView *newView = [[UIView alloc] initWithFrame:newSize];
newView.backgroundColor = [UIColor redColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[newView addSubview:button];
[self.view addSubview:newView];
}
}
}
I hope it will work as desired, now I will need to add more buttons and to change all CGRectMake params, to fit all the buttons inside the UIView.
Now, that I have this done, I would ask you only this question:
How to setup the UIView coordinates to put it like the picture, that means just above the selected row?
1 . You can get the position of the cell on screen with:
CGRect positionOnScreen = [tableView convertRect:self.frame toView:parentViewController.view];
where self is the UITableViewCell and parentViewController is the the UIViewController containing the table. You need to do this in your custom cell. Then you can pass this value to your parent UIViewController. Add your pop-up view to your UIViewController using:
[myPopUpView setFrame: CGRectMake(myPopUpView.Frame.Origin.x , positionOnScreen.origin.y - myTableCell.Frame.Size.Heigh - myCustomCell.Frame.Size.Height, myPopUpView.Frame.Size.Width, myPopUpView.Frame.Size.Width)];
myPopUpView.Alpha = 1;
I assumed you had already created the pop-up view in your .XIB with the Alpha property equals 0.
2 . You can 'close' the pop-up by hiding it with:
myPopUpView.Alpha = 0;
You can also use addSubView: and removeFromSuperview for adding/removing the pop-up view instead of creating it in the .XIB and using the Alpha property.

adding multiple subviews in ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.
Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

Resources