Using UIScrollView with paging enabled - ios

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!

Related

Tap on ScrollView calls `scrollViewWillBeginDecelerating` when the scrollView is set pagingEnabled to YES

When i tap on ScrollView it call methods scrollViewWillBeginDecelerating and scrollViewDidEndDecelerating even after the scrollView's is set pagingEnabled to YES
I have scrollView with subViews and trying to set contentOffset according to subViews. But it doesn't work.
Here is my code:
self.myScrollView.frame = CGRectMake(0, 0, pageWidth, pageHeight);
self.myScrollView.contentSize = CGSizeMake(pageWidth * self.contentViewControllers.count, pageHeight);
for (NSInteger i = 0; i < self.contentViewControllers.count; i++) {
UILabel *label = [UILabel new];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:#"Page %ld",i];
label.backgroundColor = [UIColor randomColor];
label.frame = CGRectMake(i * pageWidth, 0, pageWidth, pageHeight);
[self.myScrollView addSubview:label];
}
self.myScrollView.contentInset = UIEdgeInsetsMake(120, 0, 0, 0);
self.myScrollView.contentOffset = CGPointMake(0, - 120);
self.myScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(120, 0, 0, 0);
- (UIScrollView *)myScrollView{
if (!_myScrollView) {
_myScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
_myScrollView.backgroundColor = [UIColor blackColor];
_myScrollView.pagingEnabled = YES;
_myScrollView.directionalLockEnabled = YES;
_myScrollView.delegate = self;
//_myScrollView.delaysContentTouches = NO;
//_myScrollView.scrollsToTop = NO;
}
return _myScrollView;
}
When I tap on any subView inside the scrollView, it calls the delegate methods:
scrollViewWillBeginDecelerating >> scrollViewDidScroll >> scrollViewDidEndDecelerating
And the contentOffset change to {0,0}
Please Update Your Code like this:
Please set contentSize after for loop.
self.myScrollView.frame = CGRectMake(0, 0, pageWidth, pageHeight);
for (NSInteger i = 0; i < self.contentViewControllers.count; i++) {
UILabel *label = [UILabel new];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:#"Page %ld",i];
label.backgroundColor = [UIColor randomColor];
label.frame = CGRectMake(i * pageWidth, 0, pageWidth, pageHeight);
[self.myScrollView addSubview:label];
}
self.myScrollView.contentInset = UIEdgeInsetsMake(120, 0, 0, 0);
self.myScrollView.contentOffset = CGPointMake(0, - 120);
self.myScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(120, 0, 0, 0);
self.myScrollView.contentSize = CGSizeMake(pageWidth * self.contentViewControllers.count, pageHeight);
- (UIScrollView *)myScrollView{
if (!_myScrollView) {
_myScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
_myScrollView.backgroundColor = [UIColor blackColor];
_myScrollView.pagingEnabled = YES;
_myScrollView.directionalLockEnabled = YES;
_myScrollView.delegate = self;
//_myScrollView.delaysContentTouches = NO;
//_myScrollView.scrollsToTop = NO;
}
return _myScrollView;
}

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

iOS FadeIn/FadeOut during paging

I have an UIScrollView with views inside that I can scroll horizontally. During the scroll I would like to add a fade out on the view which is going out of the screen and a fade in on the view which is going in the screen.
Here my code :
for (int i = 0; i <textEntries.count; i++)
{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i + 5;
frame.size.width = self.scrollView.frame.size.width - 10;
frame.size.height = self.scrollView.frame.size.height - 10;
self.scrollView.pagingEnabled = YES;
UIView *subview = [[UIView alloc] initWithFrame:frame];
if([[colorEntries objectAtIndex:i] isEqualToString:#"violet"]){
subview.backgroundColor = [UIColor colorWithRed:(147/255.0) green:(90/255.0) blue:(255/255.0) alpha:1];
} else {
subview.backgroundColor = [UIColor colorWithRed:(100/255.0) green:(166/255.0) blue:(255/255.0) alpha:1];
}
subview.layer.cornerRadius = 5;
[self.scrollView addSubview:subview];
UITextView *noteLabel = [[UITextView alloc] init];
noteLabel.frame = CGRectMake(0, 0, self.scrollView.frame.size.width - 20, self.scrollView.frame.size.height -20);
noteLabel.text = [textEntries objectAtIndex:i];
noteLabel.userInteractionEnabled = NO;
noteLabel.backgroundColor = [UIColor clearColor];
noteLabel.contentInset = UIEdgeInsetsMake(20,20,20,20);
noteLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:40];
[noteLabel setTextColor:[UIColor whiteColor]];
[subview addSubview:noteLabel];
[noteLabel addObserver:self forKeyPath:#"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}
Could you help me to build this fadeIn/FadeOut effect ?
Thanks!

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.

iOS Image View turn off selection highlighting

Bit of a strange question, so i've attached a screen recording to help...
Video : https://www.dropbox.com/s/3aaefixsk8eejln/Error.mov (See past the watermark!)
My issue is that in my application when the user is at the "Preview" view and is reviewing their images, each time the image is selected, the image receives a touched overlay, which could be confusing for a user as nothing happens when touched.
I would like to disable this if possible..
Below is the example code to how the images are being displayed..
- (void)setReviewImages
{
continueButtonDisabled = YES;
downloadedImageCount = 0;
NSArray *reviewViews = scrollView.subviews;
for (IMReviewView *reviewView in reviewViews) {
[reviewView removeFromSuperview];
}
NSUInteger photoCount = [[IMLocalUser localUser] cachedPhotoCount];
if ( nPageNum == 0 ){
for (NSUInteger i = 0; i < photoCount; i++) {
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 65;
frame.size = CGSizeMake(scrollView.frame.size.width, 327.0f);
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:i];
[scrollView addSubview:subview];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * i, scrollView.frame.size.height);
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
[self.view addSubview:headingLabel];
headingLabel.text = #"Time To Preview!";
headingLabel.textColor = [UIColor blackColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.textAlignment = NSTextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blackColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
}
It seems to me like IMReviewView has a UIButton subview. If so, try to set its adjustsImageWhenHighlighted property to NO.

Resources