How to control ScrollView, that doesn't be repreated again and again - iphone

Do you know, how to control the ScrollView, so they do not repeat. For example I have 5 pictures (like the added figure), they should be viewable from 1-5 and it should stop right after the last picture, at the same time it should be able to scroll back and stop right after the first picture. All in all it should only be able to go from 1-5 without repeating after 5th picture and before 1st.
The problem right now is, that it scroll further after 5th, so it begins from 1st picture again, and the other way around when it scroll back to 1st picture it keep going to 5th and so on.
The code that I used for this is:
"InfinitePagingView" page!.
Disabled scroll:
scrollEnabled = NO;
What I want to know is, if I am able to stop scrolling right after 5th picture, but it should only be enable to do that on the right side. So is there any codes like: "scrollEnable right = NO & scrollEnable left = NO". If not, which code can I use.
Advance thanks

so you want infinite scrolling on the right side, but not on the left? You need to customize the InfinitePagingView, that you are using. Look in the code, especially the method scrollView:didScroll and disable it for the left side.
That is not a default behaviour, so there is not a simple property to set. But don't you think this is a strange behaviour? If you scroll right and go over the 5th picture, than you are at the 1st one again and you cannot scroll to the left anymore? Although you just came from that place?
If you don't want any infinite scrolling just use a default scrollView!

I have using following Code to display 5 UIButtons in Scrollview it works for as you want.you can use it for 5 pictures Check it.
- (void)viewDidLoad
{
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 407, 320, 80)];
[self.view addSubview:scrollview];
int i;
int spacex = 5;
int spacey=5;
for ( i = 0; i <5; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn = [[UIButton alloc] initWithFrame:CGRectMake(spacex, spacey, 115, 45)];
btn.backgroundColor = [UIColor greenColor];
[btn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
spacex = spacex + 120;
[scrollview addSubview:btn];
}
scrollview.contentSize = CGSizeMake(scrollview.frame.size.height*(i+2) , scrollview.frame.size.height);
[super viewDidLoad];
}
You can changed it according to your requirments.hope it works for you.Thanks.

Related

small horizontal scrollview with UIButtons

I want to create a small horizontal scrollview with few buttons as shown in image. User can select any one of them and details will be shown accordingly. I am not sure how to code it programmatically. Please help.
Image:- http://i.stack.imgur.com/uDqkn.jpg
ok follow steps to implement the same.
step 1 : take UITableview and apply affin transform for 90 degree.
step 2 : take a button on each cell also apply affinetransform of 90 degree.
step 3 : for each table cell height (now you can say width) will be calculated with the help of stringwidth function of NSString. so please type code in call back method (heightforRowatIndexPath).
step 4 : take layer of tableview and apply round corner property with value 10.0f
step 5 : same you can do for the button their color.
step 6: for outer arrow yo need to implement some UIScrollview module.
here all memory will be managed by tableview for n number of buttons in horizontal scrolling.
Thanks,
Please let me know for any issue
In an ARC view controller ...
#define kHEIGHT 35.0
- (void)createButtonScrollViewWithButtonTitles:(NSArray *)titles {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0,100.0,320.0,kHEIGHT)];
CGFloat buttonPositionX = 0.0;
for (NSString *title in titles) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(pressedButton:) forControlEvents:UIControlEventTouchDown];
[button setTitle:title forState:UIControlStateNormal];
button.frame = CGRectMake(buttonPositionX, 0.0, 90.0, kHEIGHT);
buttonPositionX += 90.0;
[scrollView addSubview:button];
}
scrollView.contentSize = CGSizeMake(buttonPositionX, kHEIGHT);
[self.view addSubview:scrollView];
}
After you get this working, you can create a version that takes an array of images. For that, the button type will change to UIButtonTypeCustom. You'll use setImage: rather than setTitle: and you'll have the chance to vary the width by advancing buttonPositionX by the image.size.width, rather than a constant.
Not sure how to interpret the little scroll-arrow-looking things on each side of the image you posted. Another subsequent enhancement would be to overlay each end with a little image view (with userInteractionEnabled = NO) that matches those arrow things.

iPad : UIButton requires many tap attempts to caught action

I am developing a Universal app, and there is a UIButton in it which works fine with iPhone but when I click it in iPad it requires many click attempts to get the touch event occur.
e.g. after 5-6 clicks it executes click event.
Below is UIButton code. Please help.
UIView *footer = [[[UIView alloc] initWithFrame:(CGRectMake(0, 0, self.tableView.frame.size.width, 54))] autorelease];
float buttonWidth = (int)((self.tableView.frame.size.width - 12 - 12) / 3);
float buttonHeight = 44;
if (clientState.Devicetype == 1) // 1=Ipad
buttonHeight = 90;
cash = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
cash.frame = CGRectMake(6, 10, buttonWidth, buttonHeight);
[cash setTitle:#"Cash" forState:UIControlStateNormal];
[cash setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
[cash addTarget:self action:#selector(handleCash:) forControlEvents:UIControlEventTouchUpInside];
cash.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
if (clientState.Devicetype == 1) // 1=Ipad
{
cash.titleLabel.font = [UIFont systemFontOfSize:28];
}
[footer addSubview:cash];
self.tableView.tableFooterView = footer;
You should check to make sure that your buttons are the correct size:
[cash sizeToFit]
I've experienced that if some other view is blocking it (and you click on that part of your button it won't fire off the event) it will take several clicks because you eventually press the button where it's not being covered. So make sure no other frame/bounds is over the top of your button and make sure your button is sized correctly.
I agree with #Inturbidus that it's probably another transparent view covering part of the button. I find the best way to troubleshoot this is to change the background color on all my other views (scrollviews, etc) to make sure they are behaving like I think they are. Sometimes it will surprise you when you see where they are actually positioned with background color turned on.

UIButton does not respond to touch events after changing its position using setFrame

I have a view controller class (child) which extends from view controller class (parent). In the parent class's loadView() method I create a sub-view (named myButtonView) with two buttons (buttons are horizontally laid out in the subview) and add it to the main view. In the subclass I need to shift these two buttons up by 50pixels.
So, I am shifting the buttonView by calling the setFrame method. This makes the buttons shift and render properly but they do not respond to touch events after this. Buttons work properly in the views of Parent class type. In the child class type view also, if I comment out the setFrame() call the buttons work properly.
How can I shift the buttons and still make them respond to touch events?
Any help is appreciated.
Following is snippets of the code.
In the parent class:
- (void)loadView {
// Some code...
CGRect buttonFrameRect = CGRectMake(0,yOffset+1,screenRect.size.width,KButtonViewHeight);
myButtonView = [[UIView alloc]initWithFrame:buttonFrameRect];
myButtonView.backgroundColor = [UIColor clearColor];
[self.view addSubview:myButtonView];
// some code...
CGRect nxtButtonRect = CGRectMake(screenRect.size.width - 110, 5, 100, 40);
myNxtButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myNxtButton setTitle:#"Submit" forState:UIControlStateNormal];
myNxtButton.frame = nxtButtonRect;
myNxtButton.backgroundColor = [UIColor clearColor];
[myNxtButton addTarget:self action:#selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButtonView addSubview:myNxtButton];
CGRect backButtonRect = CGRectMake(10, 5, 100, 40);
myBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myBackButton setTitle:#"Back" forState:UIControlStateNormal];
myBackButton.frame = backButtonRect;
myBackButton.backgroundColor = [UIColor clearColor];
[myBackButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButtonView addSubview:myBackButton];
// Some code...
}
In the child class:
- (void)loadView {
[super loadView];
//Some code ..
CGRect buttonViewRect = myButtonView.frame;
buttonViewRect.origin.y = yOffset; // This is basically original yOffset + 50
[myButtonView setFrame:buttonViewRect];
yOffset += KButtonViewHeight;
// Add some other view below myButtonView ..
}
Button may have overlapped with another view after changing the frame... Just try by setting the background color to the views which are transparent so you can get clear idea which view is overlapping on button.
Make sure your UIButton’s container view can be interacted with. This is the one that gets me. If your UIButton is a subview of another view (not your view controller’s main view), it may not be configured to allow user interaction (the default). Try: containerView.userInteractionEnabled = YES;
Thanks for the help guys, you were right. I forgot to resize the scrollView that I added above the buttons, in the parent view. So, it was overlapping with the shifted buttonView. Changing the background color helped me see it. Thanks for the tip, Chandan.
This should work, my guess and this is just off the top of my head, is that you've got something intercepting your touch events. Basically, there's another view on top of your button.
Good luck.

How to add Activity Indicator as subview to UIButton?

Can we add activity indicator as subview to UIButton?
If yes then plz tell me how to do that?
I used [button addSubview:activityIndicator];
Not working...
I found that adding a UIActivityIndicatorView to a UIButton was a really useful method to allow users to know something is happening without having to use the MBProgressHUD (I think the HUD is really good but should not be used in all situations.
For this reason I created two functions:
I have already allocated my UIButton so it is a class variable called _confirmChangesButton
I then create my activity indicator, set its frame (taking into account the button size) and then adding the indicator is easy.
- (void)addActivityIndicatorToConfirmButton {
// Indicator needs to be in the middle of the button. So half the screen less half the buttons left inset less half the activity indicator size
CGRect rect = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 10 - 15, 5, 30, 30);
UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc] initWithFrame:rect];
activity.hidesWhenStopped = YES;
[_confirmChangesButton setTitle:#"" forState:UIControlStateNormal];
[_confirmChangesButton addSubview:activity];
[activity startAnimating];
}
Having a removal function is also useful if you are using blocks. It might be that the completion task comes back with a failure and so we want to remove the indicator and change the title back. In this function we need to make sure to remove the indicator and not the button label which is the other subview on this button.
- (void)removeActivityIndicatorFromConfirmButton {
UIActivityIndicatorView * activity = _confirmChangesButton.subviews.lastObject;;
[activity removeFromSuperview];
[_confirmChangesButton setTitle:#"Confirm Change" forState:UIControlStateNormal];
}
I found that using these two you can create a much better user experience letting the user know what is going on when they press buttons.
Hope this helps
Use the below code below to add acitivity indicator a button or any uiview object
UIActivityIndicatorView *aView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
aView.frame = CGRectMake(0, 0, {yourButton}.frame.size.width, {yourButton}.frame.size.height);
aView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[{yourButton} addSubview:aView];
[aView startAnimating];
Hope this will help..
I don't think it's possible to add a view to a button. UIButton have this method because it's inherited from UIVIew.
The real question is : why do you want to add an activity indicator on a button and not elsewhere ?
did you do [activityIndicator startAnimating]; ALso as u are using it in a tableview just check if the tags are set properly

Hit target on UIToolbar with custom-image buttons is not correct

I have a UIToolbar that I've customized with my own background image. Consequently, the built-in UIBarButtonItem appearance doesn't work for me, so I'm using images that are already prepared to show in the bar. I create a custom button item with this method:
+ (UIBarButtonItem *)customWithImage:(UIImage *)image enabled:(BOOL)enabled target:(id)target action:(SEL)selector {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//I've tried different values for the frame here, but no luck
button.frame = CGRectMake(0, 0, 44, 44);
button.enabled = enabled;
button.showsTouchWhenHighlighted = YES;
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
[button setImage:image forState:UIControlStateNormal];
UIBarButtonItem *it = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
//Tried changing this, to no avail
it.width = 32.f;
return it;
I have one button on the left and one on the right and I'm trying to make it so that if you tap on the far left or far right of the UIToolbar, the corresponding button is tapped. However, with these custom buttons, the hit targets do not extend all the way to the edges of the UIToolbar, but are inset from the sides:
http://skitch.com/andpoul/d1p8g/hit-targets
Any help greatly appreciated!
UIBarButtonItem.width might be ignored if you're using a custom view (it probably just uses the width of the view).
A lame hack is to make the toolbar wider (so it sticks outside the screen) and add transparent edges to the background image to compensate. This brings the buttons closer to the edge of the screen.
An alternative is just to use a UIImageView with UIButton subviews.
I think the only way you will have to go is to make buttons wider (change image by adding some from left for one and right for another) and adjust size...