Why isn't my UIPageControl updating when the ScrollView scrolls? - ios

I'm trying to make a table whose cells have UIPageControls with ScrollViews in them. The ScrollViews work and the PageControls are there, but the PageControl does not update when scrolling happens except for on the bottom cell. Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.contentView.backgroundColor = [UIColor blueColor];
UIColor *color = [colorsArray objectAtIndex:indexPath.row];
scrollView = [[UIScrollView alloc] init];
[scrollView setDelegate:self];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = tableView.rowHeight;
scrollView.frame = CGRectMake(0, 0, width, height/1.25);
scrollView.contentSize=CGSizeMake(width*3,height/1.25);
scrollView.pagingEnabled = YES;
[scrollView setBackgroundColor:color];
[scrollView setShowsHorizontalScrollIndicator:NO];
scrollView.clipsToBounds = YES;
NSUInteger i;
int xCoord=width/25;
int yCoord=width/25;
int buttonWidth=width/8;
int buttonHeight=width/8;
int buffer = width;
for (i = 1; i <= 4; i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.backgroundColor = [UIColor blackColor];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
aButton.tag = i;
UIImage *buttonImage = [picsArray objectAtIndex:indexPath.row];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[scrollView addSubview:aButton];
xCoord += buttonHeight + buffer;
}
[cell addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, height/1.25, width, height/4)];
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
pageControl.userInteractionEnabled =YES;
[pageControl addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:pageControl];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
- (void)pageAction:(UIPageControl *)sender {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = scrollView.frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
[ scrollView scrollRectToVisible:frame animated:YES];
}

That is because you have reference only to scroll view and page control generated for the last cell:
// this references are redefined every time new cell is generated
scrollView = [[UIScrollView alloc] init];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, height/1.25, width, height/4)];
You could try to use sender in your UIScrollView delegate method:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = sender.frame.size.width;
int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
// !!! but here is another problem. You should find reference to appropriate pageControl
pageControl.currentPage = page;
}
But there will be one more problem: you should get reference to the appropriate UIPageControl object. You could use tags, array or anything else for that purpose.

Related

UIButton not displayed

I use the following code to add a UIButton and a UITableView to a UIViewController:
self.tableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.scrollEnabled = NO;
tableView.allowsSelection = NO;
tableView.dataSource = self;
tableView.delegate = self;
tableView;
});
self.refreshButton = ({
UIButton *button = [UIButton new];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle:#"testtest" forState:UIControlStateNormal];
[button addTarget:self
action:#selector(clickRefreshButton)
forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.numberOfLines = 1;
button.titleLabel.textAlignment = NSTextAlignmentRight;
button.titleLabel.adjustsFontSizeToFitWidth = NO;
button.alpha = 1.0;
button;
});
[self.view addSubview:self.tableView];
[self.view addSubview:self.refreshButton];
CGFloat tableViewUpPadding = 20.0f;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
CGFloat buttonOffset = 10.0f;
CGFloat buttonSideLength = 50.0f;
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view.mas_top).offset(tableViewUpPadding);
make.bottom.equalTo(self.view.mas_bottom).offset(-tabBarHeight);
make.width.equalTo(self.view.mas_width);
make.left.equalTo(self.view.mas_left);
}];
[self.refreshButton mas_makeConstraints:^(MASConstraintMaker *make){
make.right.equalTo(self.view.mas_right).offset(-buttonOffset);
make.bottom.equalTo(self.view.mas_bottom).offset(-buttonOffset);
make.width.equalTo(#(buttonSideLength));
make.height.equalTo(#(buttonSideLength));
}];
Among which I use masonry to do auto-layout. However when I run the app the button doesn't appear, and not even in view debugging area.. Can somebody tell me why?
Or if I use strings and struts to create button, it still won't show :(
CGRect applicationFrame = [UIScreen mainScreen].bounds;
CGFloat screenWidth = CGRectGetWidth(applicationFrame);
CGFloat screenHeight = CGRectGetHeight(applicationFrame);
CGFloat buttonWidth = 40.0f;
CGFloat buttonHeight = 40.0f;
CGFloat spacing = 10.0f;
CGFloat buttonX = screenWidth - buttonWidth - spacing;
CGFloat buttonY = screenHeight - buttonHeight - spacing;
CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
[self.refreshButton setFrame:buttonFrame];
By the way, I found that if I want to change self.tableView's width using auto-layout, it's also impossible:
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view.mas_top).offset(tableViewUpPadding);
make.bottom.equalTo(self.view.mas_bottom).offset(-tabBarHeight);
make.width.equalTo(self.view.as_width).offset(-offset);
make.left.equalTo(self.view.mas_left);
}];
It turns out that the tableview is still as wide as self.view

ScrollView for images Objective C (iOS)

I am trying to create a welcome page for my app (with screenshots).
I have taken some samples and made it into a text scroll. I have tried for weeks without any success. Thanks
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.textAlignment = UITextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
Here is my failed attempt to do with images:
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UIImageView *tmpLabel = [[UIImageView alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.imageAlignment = UIImageAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:subview1.frame];
if (i == 0) {
imageView1.image = [UIImage imageNamed:#"openingScreen1.png"];
}else if (i == 1){
imageView1.image = [UIImage imageNamed:#"openingScreen2.png"];
}else if (i == 2){
imageView1.image = [UIImage imageNamed:#"openingScreen3.png"];
}else {
imageView1.image = [UIImage imageNamed:#"openingScreen4.png"];
}
I also might need to remove the word "Parse" unless the images above sit on top of it? I cannot locate the word.
try this
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
[someScrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfPages = 5;
for (int i = 0; i < numberOfPages; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width+10;
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin,70,self.view.frame.size.width,25)];
tmpLabel.textAlignment = NSTextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i+1];
[someScrollView addSubview:tmpLabel];
// calculate the width
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, tmpLabel.frame.size.height + tmpLabel.frame.origin.y + 10,
self.view.frame.size.width,
self.view.frame.size.height)]; // customize your width, height and y co ordinates
image.image = [UIImage imageNamed:[NSString stringWithFormat:
#"openingScreen%d.jpg", i+1]];
image.contentMode = UIViewContentModeScaleAspectFit;
[someScrollView addSubview:image];
}
//set the scroll view content size
someScrollView.backgroundColor = [UIColor blueColor];
someScrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfPages,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:someScrollView];
the sample OutPut is
here I attached the sample Project also, please use this
Please change your code to something like
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
//set the scroll view delegate to self so that we can listen for changes
someScrollView.delegate = self;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 1; i <= numberOfPages; i++) {
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//get the sub view and set the frame
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, someScrollView.frame.size.height)];
imageView1.image = [UIImage imageNamed: [NSString stringWithFormat:#"openingScreen%d.png",i ]];
//add the subview to the scroll view
[someScrollView addSubview:imageView1];
}
Add scroll view Delegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
//dragging ends, please switch off paging to listen for this event
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *) targetContentOffset NS_AVAILABLE_IOS(5_0)
{
//find the page number you are on
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(#"Dragging - You are now on page %i",page);
}

Quick movement change of image on swipe scrollview

In the below code when clicked on page control it moving to another Image, with an quick movement.
Need same effect of quick movement swipe on scroll View.
-(void)viewDidLoad{
pageControlBeingUsed = NO;
pageControl = [[UIPageControl alloc]init];
scrollView = [[UIScrollView alloc] init];
scrollView.delegate = self;
scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/1.6);
scrollView.bounces = true;
scrollView.showsHorizontalScrollIndicator = false;
scrollView.showsVerticalScrollIndicator = false;
NSArray *colors = [NSArray arrayWithObjects:[UIColor colorWithPatternImage:[UIImage imageNamed:#"image1"]], [UIColor colorWithPatternImage:[UIImage imageNamed:#"image2"]],[UIColor colorWithPatternImage:[UIImage imageNamed:#"image3"]],nil];
for (int i = 0; i < colors.count; i++){
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
}
scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
pageControl.frame = CGRectMake(0,self.scrollView.frame.size.height+10, self.view.frame.size.width,30);
[pageControl addTarget:self action:#selector(changePage:) forControlEvents:UIControlEventValueChanged];
pageControl.currentPage = 0;
pageControl.numberOfPages = colors.count;
[self.view addSubview:pageControl];
[self.view bringSubviewToFront:pageControl];
[self.view addSubview:scrollView];
}
- (void)scrollViewDidScroll:(id)sender {
if (!pageControlBeingUsed) {
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)changePage:(id)sender{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
From above code when i click on pageControl its trigger the method changePage and move to next page, same effect need when user swipe from left to right right to left on scrollview.
#All
Just Add scrollView.pagingEnabled = YES; Issue resolved.

Using UIScrollView with paging enabled

I have implemented paging using UIScrollView and adding UIVIew and UIControls in this UIVIew on each pages. Surprisingly none of the UIControl responding when it is on UIScrollView with paging enabled! Following is my code. Could anyone please tell me how to get UIControls working on UIScrollView with paging enabled?
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
int totalPages = [[delegate sections] count] + 2; // 2 for startOfDayQuestions and endOfDayQuestions
int X = 45, Y = 20, H = 40, W = 680;
// start of the day questions
CGRect startFrame = scrollView.frame;
startFrame.origin.x = 0;
startFrame.origin.y = 0;
UIView *startPage = [[UIView alloc] initWithFrame:startFrame];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, W, H)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.text = #"Text";
label.font = [UIFont fontWithName:paragraph_font_name size:paragraph2_font_size];
[startPage addSubview:label];
Y += H;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(X, Y, W, H)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
textField.userInteractionEnabled = YES;
[startPage addSubview:textField]; --> This UITextField doesn't respond when added to startPage and in turns scrollView but works fine if I add it to self.view!
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.userInteractionEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * totalPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = totalPages;
pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (pageControlUsed)
return;
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender
{
int page = pageControl.currentPage;
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
Adding subScrollView with no pagingEnabled to each page and controls into subScrollView works! If you add controls directly to scrollview with paging enabled, it seems gesture recogniser's first responder is always scrollview so your controls never gets the event and behave like a disabled!

How could I make this work with UIScrollView?

I'm inserting dynamically anywhere from 1 to 300 buttons inside a UIScrollView(as subviews). Currently, the buttons line up nicely but is not scrolling as I'd like them to. It just scrolls horizontally without a paging effect. Ideally, they way I'd like it to work is:
Display a max of 48 buttons per "page"
When scrolling to the right, show next "page" with 48 additional buttons
My code:
-(void)populateScrollView:(NSMutableArray *)colors {
// Properties
NSInteger count = 0;
NSInteger rowsize = 48;
CGFloat pageNum = 6;
CGFloat pageWidth = scrollView.frame.size.width;
CGFloat pageHeight = scrollView.frame.size.height;
CGFloat visibleWidth = 465;
myButtons = [[NSMutableArray alloc]init];
// Clear the subviews if any
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (Color *element in colors) {
// Set the button properties
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:#"color_overlay.png"];
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateHighlighted];
div_t temp = div(count, rowsize);
button.frame = CGRectMake(52 * temp.rem , 52*temp.quot, 52, 52);
button.tag = count++;
UIImage *effect = [UIImage imageNamed:element.image_resource];
effectImage = [[UIImageView alloc] initWithImage:effect];
effectImage.frame = CGRectMake(52 * temp.rem , 52*temp.quot, 52, 52);
// Color the graphic if color available. If not, load the image
unsigned result = 0;
if ([element.hex_value length] > 1){
NSScanner *scanner = [NSScanner scannerWithString:element.hex_value];
[scanner scanHexInt:&result];
button.backgroundColor = UIColorFromRGB(result);
}
else {
effectImage.image = effect;
}
// Set actions for the color
[button addTarget:self action:#selector(changeGraphicColor:) forControlEvents:UIControlEventTouchUpInside];
[myButtons addObject:button];
for (UIButton *myButton in myButtons){
[scrollView insertSubview:effectImage atIndex:1];
[scrollView insertSubview:myButton atIndex:2];
}
}
// Set scrollView contentSize and create pages:
CGFloat leftShift=floor(((visibleWidth-pageWidth)/2)/pageWidth)*pageWidth;
CGFloat contentSizeReduction=2*leftShift;
scrollView.contentSize = CGSizeMake(pageNum*pageWidth-contentSizeReduction,pageHeight);
[myButtons removeAllObjects];
[myButtons release];
}
I'm thinking the way I'm adding the buttons won't allow me to implement what I am trying to accomplish. What do you guys think I'm doing wrong here?
Did you set scrollView.pagingEnabled to YES?

Resources