Scroll view button action is not working when animating - ios

Added 10 UIButtons in a UIScrollView. Wrote automatically scroll the scrollview code using UIAnimation.
for (int i=0; i<10; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(XPosition, YPosition, width,50);
[button setTitle:#"Warning animation items array " forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
if (index%2) {
button.backgroundColor = [UIColor blueColor];
} else{
button.backgroundColor = [UIColor purpleColor];
}
XPosition = paddingSpace + XPosition + button.frame.size.width;
[_scroll addSubview:button];
index++;
}
_scroll.contentSize=CGSizeMake(XPosition,50);
When animating the UIScrollView, I can't able tap any UIButton!!!
[UIView animateWithDuration:20
delay:0.0
options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat)
animations:^{
_scroll.contentOffset = CGPointMake(XPosition, 0.0);
} completion:^(BOOL finished){}];
If the animation is stop only I can tap the UIButton.
Please help me out this.

you can use scrollview.userintreactionenable=yes;
i think this help for you

Related

getting element from array one by one and createbutton

I have a UItextFeild as search bar on clicking a tableview with suggestion open.now i want to click on that row and make a button with its label in below view as described in image .how can i acheive this
I want it one by one means when i click on row it create a button on view and UITableView hide then again i start searching and on again clicking on some other row make a button with different value of X.
_selectednames = [NSMutableArray arrayWithCapacity:self.sortedArray.count];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// MGContactItemsModel *model = _arrayForContacts[indexPath.row];
self.searchText.text = [NSString stringWithFormat:#"%#",[_sortedArray objectAtIndex:indexPath.row]];
self.tableView.hidden = YES;
[_selectednames addObject:_searchText.text];
for (int i; i < _selectednames.count ; i++) {
[self makeLabelsAndButtons:i];
}
now in my makeLabelsAndButtons funtion i done something like this
CGFloat xOffset = 10.0f;
int buttonCount = 0;
CGRect buttonFrame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
NSString *nameString = [self.selectednames objectAtIndex:indexPath];
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//_button.frame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
for(int i=buttonCount; i >0;i++) {
xOffset = xOffset+10;
buttonFrame.origin.x += buttonFrame.size.width+xOffset;
}
_button.frame = buttonFrame;
buttonCount++;
_button.tag = indexPath;
//button.center = CGPointMake(xOffset, 10.0f);
//[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[_button setTitle:self.selectednames[indexPath] forState:UIControlStateNormal];
_button.backgroundColor = [UIColor lightGrayColor];
_button.titleLabel.font = [UIFont fontWithName:#"Arial" size:8.0f];
_button.layer.cornerRadius = 10.0f;
_button.layer.masksToBounds = YES;
[self.buttonView addSubview:_button];
NSLog(#"name: %#", nameString);
also on clicking button it will removed.any help would be thankful.
It works for me.Please try it.
y=20;
x=20;
for (int i=0; i<[arrAssignUsers count]; i++) {
CGRect screenRect=[[UIScreen mainScreen]bounds];
CGFloat screenWidth=screenRect.size.width;
[arrBtnStatus addObject:[NSNumber numberWithBool:NO]];
NSString *strNames=[arrAssignUsers objectAtIndex:i];
stringsize=[strNames sizeWithAttributes:
#{NSFontAttributeName: [UIFont systemFontOfSize:20.0f]}];
btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat m=x+stringsize.width+10;
CGFloat n=screenWidth-20;
if (m<=n) {
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width +10;
}
else
{
y=y+stringsize.height+10;
x=20;
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width+10;
}
[btn setTitle:self.arrAssignUsers[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.tag=i;
[btn addTarget:self
action:#selector(notifyButtonPressedInEditTask:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor=WHITE_COLOR;
btn.layer.cornerRadius=10;
[btn.layer setMasksToBounds:YES];
btn.layer.borderWidth=2.0f;
btn.layer.borderColor=[UIColor orangeColor].CGColor;
[self checkNotifybtnStatus:btn];
[cell addSubview:btn];
}
}
It may helps to you.Thank you
You can use UICollectionView to create buttons. All you need to do is
1. Create custom cell with button as you want
2. When you tap on table view cell, add item to array and reload UICollectionView
3. Show buttons and it will automatically add scrollview for you.
When you tap on UICollectionView cell, remove object from array and reload UICollectionView again.
This will more easy as compared to adding buttons and setting frames.
And design UICollectionView cell in such way that it will same length as your text, use NSLayoutConstraints for it. It will work fast and smooth.
Code will be minimum and reusable.
Try out this working code...
Take UIScrollView in your Xib for buttons
//Adding menu buttons
btnArray = #[#"SYMPTOMATIC", #"SOCIAL", #"VOCATIONAL", #"PERSONAL"];
[self addButtonsInScrollMenu:btnArray];
/**
* Add Menu Buttons in Menu Scroll View
*
* #param buttonArray Array containing all menu button title
*/
#pragma mark - Add Menu Buttons in Menu Scroll View
-(void) addButtonsInScrollMenu:(NSArray *)buttonArray
{
CGFloat buttonHeight = self.menuScrollView.frame.size.height;
CGFloat cWidth = 0.0f;
for (int i = 0 ; i<buttonArray.count; i++)
{
NSString *tagTitle = [buttonArray objectAtIndex:i];
CGFloat buttonWidth = [self widthForMenuTitle:tagTitle buttonEdgeInsets:kDefaultEdgeInsets];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(cWidth, 0.0f, buttonWidth, buttonHeight);
[button setTitle:tagTitle forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"Roboto-Regular" size:12.0f];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.menuScrollView addSubview:button];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.size.height - kButtonBorderHeight, button.frame.size.width, kButtonBorderHeight)];
bottomView.backgroundColor = [UIColor darkTextColor];
bottomView.tag = 1001;
[button addSubview:bottomView];
if (i == 0)
{
button.selected = YES;
[bottomView setHidden:NO];
}
else
{
[bottomView setHidden:YES];
}
cWidth += buttonWidth;
}
NSLog(#"scroll menu width->%f",cWidth);
self.menuScrollView.contentSize = CGSizeMake(cWidth, self.menuScrollView.frame.size.height);
}

how to make UIView work as an scrollview

I need some serious help on this problem of mine where I need to work a UIView as an ScrollView. Below uploaded picture will clear all the doubts.
I have taken four buttons along with a UIView below them along with 4 container views which I have placed one above the other so as to work along with the four buttons.. What I want now is to work UIView as an ScrollView. Whenever I click any button, the View moves forward as well as the it changes the container view. And respective container view shows whenever I click a button.
Hi better you can use UIScrollView instead of UIView like:
//Declare it
NSMutableArray * buttonArray
int selectedIndex;
-(void)setUpSegmentBar {
NSArray * namesOfMenus =#[#"Button1",#"Button2",#"Button3",#"Button4",#"Button5"];
// Scrollview added on storyBoard
myScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 2, self.view.frame.size.width, 40)];
CGFloat scrollWidth = 0.f;
buttonArray=[[NSMutableArray alloc]init];
for ( int j=0; j<namesOfMenus.count; j++)
{
NSString * name =[namesOfMenus objectAtIndex:j];
CGSize size = [name sizeWithAttributes:
#{NSFontAttributeName: [UIFont fontWithName:font_bold size:font_size_button]}];
CGSize textSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
CGFloat strikeWidth;
if (iPAD) {
strikeWidth = self.view.frame.size.width/5.5;
}else{
strikeWidth = textSize.width;
}
CGRect frame = CGRectMake(scrollWidth, 0,strikeWidth+20, 40);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:j];
button.frame = frame;
[button setBackgroundColor:[UIColor whiteColor]];
button.titleLabel.textColor=[UIColor whiteColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.layer.borderColor=[UIColor whiteColor].CGColor;
button.titleLabel.textAlignment=NSTextAlignmentCenter;
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:name forState:UIControlStateNormal];
// ScrollWidth As per Button Width
scrollWidth= scrollWidth+strikeWidth+20;
// to Check Which Button Is Selected.
if (j==selectedIndex) {
// If Selected Do UI Changes
button.backgroundColor=segment_selected_Color ;
[button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
[button addShaddow];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}else {
// Other Buttons
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
button.backgroundColor=segment_disselected_Color;
[button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else{
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}
// Add Buttons in Array
[buttonArray addObject:button];
// Add button on Scroll View
[myScrollView addSubview:button];
}
myScrollView.contentSize = CGSizeMake(scrollWidth, 30.f);
myScrollView.pagingEnabled = NO;
myScrollView.backgroundColor=[UIColor whiteColor];
[myScrollView setShowsHorizontalScrollIndicator:NO];
[myScrollView setShowsVerticalScrollIndicator:NO];
return myScrollView;
}
And For Handeling button action.
-(void)buttonEvent:(UIButton*)sender
{
NSInteger index= sender.tag;
selectedIndex= (int) index;
for(int i=0;i<buttonArray.count;i++)
{
UIButton * button =(UIButton*)[buttonArray objectAtIndex:i];
if (i==selectedIndex) {
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
button.backgroundColor=segment_selected_Color ;
[button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
[button addShaddow];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}else {
button.backgroundColor=segment_disselected_Color;
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
[button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else{
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}
}
CGRect frame1 = myScrollView.frame;
UIButton * bt=(UIButton*)[buttonArray objectAtIndex:index];
frame1 =bt.frame ;
// By using it button will scroll and show properly
[myScrollView scrollRectToVisible:frame1 animated:YES];
[self setupTableAfterClick];
}

iOS UIButton can't click in UIScrollView

I had search some post before. But I can't try out the correct result.
I generate more button and add the view in the UIScrollView.
I had set autolayout in storybaord about the view and scrollview.
But the Button click are not listener. I don't know why.
I had set
_sclVW.delaysContentTouches = NO;
[_sclVW setCanCancelContentTouches:NO];
_sclVW.userInteractionEnabled = YES;
But the button still can't click.
My code is below:
_sclVW.delegate = self;
_sclVW.delaysContentTouches = NO;
[_sclVW setCanCancelContentTouches:NO];
_sclVW.userInteractionEnabled = YES;
int btnWidth = 80,btnHigh=50;
for( int i = 0 ; i < 50 ; i++ )
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(btnSendTag:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
button.tag = i;
button.titleLabel.numberOfLines = 0;
button.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.userInteractionEnabled = YES;
[button setEnabled:YES];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor darkGrayColor]];
if( i < 3)
{
button.frame = CGRectMake( i *(btnWidth + 20), 0 , 80,50 );
}
else
{
button.frame = CGRectMake( (i%3) *(btnWidth + 20), (i/3) *(btnHigh + 20) , 80,50 );
}
[_contentVW addSubview:button];
}
}
-(void) btnSendTag:(UIButton *)sender
{
NSLog(#"btn click:%li",(long)sender.tag);
}
Have anyone know how to resolve the problem?
thank you very much.
I had post my simple test project in github(this code).
I found your issue from sample project. Issue here is height and width of contentVW in Storyboard is 0. So that subview (buttons) of this view can not be interact with user.
Look at the picture below:
Here you can see that subview of UIScrollView has value 0 for width and height.
So change width and height of view same like scrollview and than you can able to tap on these buttons.

UIScrollView needs to get bigger every time I add a button

The scrollView is created in my storyboard and is horizontal. Here is the method where I add a button for every loop and need to increase the size of the scrollView every time a button is added.
- (void) populateGroups {
NSInteger x = [self.groupNameArray count];
for (int i = 0; i < x; i++) {
NSString *name = [self.groupNameArray objectAtIndex:i];
// Create Button
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(count, 0, 100, 54)];
[button setTitle:name forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:10];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor:[UIColor whiteColor].CGColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.groupScrollView addSubview:button];
count = count + 100;
// Increase ScrollViewSize
self.groupScrollView.frame = CGRectMake(0, 0, frameWidth, 80);
frameWidth = frameWidth + 100;
}
}
The scrollView stays the same when I try to add two buttons to the view.
you have to increase the content size of the scroll view like:
self.groupScrollView.contentSize = CGSizeMake(frameWidth, 80);
and better to do it after the loop.

UIButton animation ontarget self

I am working on a app with two UIButtons which have to change size and color when they are pressed. My code is:
UIButton *Answer1Button = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *Answer2Button = [UIButton buttonWithType:UIButtonTypeCustom];
[Answer1Button addTarget:self action:#selector(Answer1Action) forControlEvents:UIControlEventTouchUpInside];
[Answer2Button addTarget:self action:#selector(Answer2Action) forControlEvents:UIControlEventTouchUpInside];
[Answer1Button setBackgroundColor:[UIColor redColor]];
[Answer2Button setBackgroundColor:[UIColor yellowColor]];
Answer1Button.frame = CGRectMake(0, 0, 320, 45);
Answer2Button.frame = CGRectMake(0, 50, 320, 45);
And the function I created for it:
-(void)Answer1Action{
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
self.backgroundColor = [UIColor greenColor];
self.alpha = 0.5;
[UIButton commitAnimations];
}
The problem I encounter right now, is when I press the UIButton the function is called but the self.alpha affects the entire UIView in which the button is located in.
I'm still a rookie with Objective-C so I'm thinking it is probably something simple I forgot.
[Answer1Button setBackgroundColor:[UIColor greenColor]];
[Answer1Button setAlpha:0.5];
//Include QuartzCore framework and import it to your viewController.h,and write the below line along with the above code to change the size and to do many animations.
Answer1Button.layer.affineTransform=CGAffineTransformScale(Answer1Button.transform, 1.2, 1.2);
You are setting self.alpha = 0.5;, here self is not your button. You should set your button:
Answer1Button.backgroundColor = [UIColor greenColor];
Answer1Button.alpha = 0.5;
But this is not the best practice. You can send your button to the handler method as param:
(Variable and method names should not start with capital letter in ObjC.)
[answer1Button addTarget:self action:#selector(answer1Action:) forControlEvents:UIControlEventTouchUpInside];
And use the sender button as parameter in your handler method:
-(void)answer1Action:(id)sender{
UIButton* myButton = (UIButton*)sender;
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
myButton.backgroundColor = [UIColor greenColor];
myButton.alpha = 0.5;
[UIButton commitAnimations];
}
Set button alpha instead of self.alpha=0.5;
-(void)Answer1Action{
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
self.backgroundColor = [UIColor greenColor];
Answer1Button.alpha = 0.5;
[UIButton commitAnimations];
}

Resources