iOS gradient animating with uiscrollview paging - ios

I have an horizontal scrollview with 3 pages.
I would like to have one background color different for each page and draw gradient between the 3 colors.
Page 1 with green, page 2 with blue and page 3 with red.
How can I do that?
I was thinking about ScrollViewDidScroll delegate method:
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = self.statisticScrollView.frame.size.width;
float fractionalPage = self.statisticScrollView.contentOffset.x / pageWidth;
NSLog(#"fractional Page: %f", fractionalPage);
NSInteger lowerNumber = floor(fractionalPage);
NSLog(#"lower Page: %i", lowerNumber);
NSInteger upperNumber = lowerNumber + 1;
NSLog(#"upper Page: %i", upperNumber);
if (self.lastContentOffset > sender.contentOffset.x){
//RIGHT --->
if (lowerNumber == 0) {
//gradient green to blue
}else if (lowerNumber == 1){
//gradient blue to red
}else if (lowerNumber == 2){
//end pages
}
}else if (self.lastContentOffset < sender.contentOffset.x){
//LEFT <----
if (lowerNumber == 0) {
//gradient blue to green
}else if (lowerNumber == 1){
//gradient red to blue
}else if (lowerNumber == 2){
//end pages
}
}
}
Can I use fractionalPage to set the percentage of the gradient?
I read about startPoint and endPoint properties of the CAGradientLayer, but It seems not working correctly.
Any suggestions will be appreciated!
Thank you in advance

The startPoint and endPoint properties are measured from 0 to 1 and NOT in the coordinate system of the view which may be an issue you are encountering. So to run the gradient from side to side you need to declare it like this.
gradlayer.startPoint = CGPointMake(0, 0.5);
gradlayer.endPoint = CGPointMake(1, 0.5);
A simple way of achieving what you want to do is to add a sublayer to the scroll view with the gradient pre-configured.
CGFloat width = CGRectGetWidth(self.view.frame);
CGFloat height = CGRectGetHeight(self.view.frame);
self.scroller.contentSize = CGSizeMake(3*width, height);
self.gradlayer = [CAGradientLayer layer];
self.gradlayer.frame = CGRectMake(0, 0, width*3, height);
[self.scroller.layer addSublayer:self.gradlayer];
self.gradlayer.colors = #[((id)[UIColor redColor].CGColor),((id)[UIColor greenColor].CGColor),((id)[UIColor blueColor].CGColor)];
self.gradlayer.startPoint = CGPointMake(0, 0.5);
self.gradlayer.endPoint = CGPointMake(1, 0.5);
However this may cause memory issues if your content size gets much larger.
I had a look on the Sim and it didn't seem grow with more pages. I went to 10 and it seemed to stay about the same as 3 pages but it is a consideration so you may have to go back to your original theory of adjusting the color array depending on the horizontal offset.
I think the scrollviews internal paging handles its sublayers effectively to avoid this.
Sweet color scheme though. Jerry Garcia would be proud.

Related

how to move one direction in UIScrollView when scrolling

I'm new in objective-c. I create UIScrollView object and add in my view with this code:
height = self.view.frame.size.height;
width = self.view.frame.size.width;
scrollbar = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
scrollbar.delegate = self;
scrollbar.backgroundColor = [UIColor whiteColor];
scrollbar.maximumZoomScale = 1.0;
scrollbar.minimumZoomScale = 1.0;
scrollbar.clipsToBounds = YES;
scrollbar.showsHorizontalScrollIndicator = YES;
scrollbar.pagingEnabled = YES;
[scrollbar setContentSize:CGSizeMake(width*4, height*4)];
[self.view addSubview:scrollbar];
for (int i = 1; i <= 4; i++) {
first = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
first.view.frame = CGRectMake((i-1)*width, 0, width, height*4);
[scrollbar addSubview:first.view];
switch (i) {
ase 1:
first.view.backgroundColor = [UIColor blueColor];
break;
case 2:
first.view.backgroundColor = [UIColor redColor];
break;
case 3:
first.view.backgroundColor = [UIColor greenColor];
break;
case 4:
first.view.backgroundColor = [UIColor grayColor];
break;
default:
break;
}
}
in my code I add 4 view in my ScrollView with different color now I want when scrolling on my ScrollView detect dx & dy (dx: driving distance on Axis.x & dy:driving distance on Axis.y) and check these two variable and when :
Notic: I want when any one touch on ScrollView and moving touch on Axis (x or y) or touch on Both Axis (x and y) check this :
if (dx > dy) disable horizontal scroll and moving in vertical side!!!
else moving in horizontal side and disable vertical scroll!!!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect visibleRect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.contentOffset.x + scrollView.bounds.size.width, scrollView.contentOffset.y + scrollView.bounds.size.height);
NSLog(#"%f,%f",visibleRect.origin.x,visibleRect.origin.y);
/*NSLog(#"x : %f",scrollView.contentOffset.x);
NSLog(#"y : %f",scrollView.contentOffset.y);*/
if (fabsf(scrollView.contentOffset.x) > fabsf(scrollView.contentOffset.y)) {
NSLog(#"Vertical Side");
}
else {
NSLog(#"Horizontal Side");
}
}
please guide me guys. I can't disable one side and move another side!!! thanks
If i understood you right, you want to disable scrolling in the direction that has been dragged less by the user. If so, UIScrollView already offers an option to do so:
scrollView.directionalLockEnabled = YES;
When this option is set to true UIScrollView will handle the correct direction lock by itself.
For more information look at the documentation: link
You can achieve the result you want adding some code to your delegate. This is my ViewController.m file. -viewDidLoad, #import statements and the other methods are omitted.
#interface ViewController () {
CGPoint initialOffset;
NSInteger direction; // 0 undefined, 1 horizontal, 2 vertical
}
#end
#implementation ViewController
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// retrieve current offset
CGPoint currentOffset = scrollView.contentOffset;
// do we know which is the predominant direction?
if (direction == 0) {
// no.
CGFloat dx = currentOffset.x - initialOffset.x;
CGFloat dy = currentOffset.y - initialOffset.y;
// we need to decide in which direction we are moving
if (fabs(dx) >= fabs(dy)) {
direction = 1; // horizontal
} else if (fabs(dy) > fabs(dx)) {
direction = 2;
}
}
// ok now we have the direction. update the offset if necessary
if (direction == 1 && currentOffset.y != initialOffset.y) {
// remove y offset
currentOffset.y = initialOffset.y;
// update
[scrollView setContentOffset:currentOffset];
} else if (direction == 2 && currentOffset.x != initialOffset.x) {
currentOffset.x = initialOffset.x;
[scrollView setContentOffset:currentOffset];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// store the current offset
initialOffset = scrollView.contentOffset;
// reset flag
direction = 0; // AKA undefined
}
#end
When you start dragging, the delegate will reset the flag direction to "unknown" state, and store the current content offset. After every dragging move, your -scrollViewDidScroll: will be called. There, you decide which is the predominant direction (if this hasn't been done yet) and correct the current scrolling offset accordingly, by removing the x (or y) offset.
I tested this with the same settings you provided, only I used a UIImageView inside UIScrollView and I set up everything via InterfaceBuilder, but it should work fine. Theoretically, with this method you could replace directionLock, but remember that -scrollViewDidScroll is called many times during an action, and every time it rewrites the content offset (if the scrolling is happening in both directions). So if you leave directionLock enabled, you save many of the calls to setContentOffset: that the delegate performs.

Horizontal parallax not working as intended

I'm attempting a parallax effect with a UIScrollView whereby there will be an UIImageView and UILabel per 'page', and as the user scrolls through the pages, the UIImageView will disappear off of the current page at a faster rate than the UILabel, while the next UIImageView and UILabel appear.
My code is working perfectly when on the first page as the amplifier is not taken into account, thus not working on any other page; the UIImageView is not centered horizontally within the page, which it should be.
Please can you tell me where I'm going wrong?
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
NSInteger currentPage = floorf(offset.x / CGRectGetWidth(self.view.frame));
if (currentPage >= 0 && currentPage < CINNumberOfWalkthroughs) {
NSInteger lastPage = self.pageControl.currentPage;
self.pageControl.currentPage = currentPage;
float imageViewAmplifier = offset.x / scrollView.contentSize.width;
UIImageView *imageView = self.imageViews[currentPage];
float w = CGRectGetWidth(self.view.frame);
float a = CGRectGetWidth(imageView.frame);
imageView.center = CGPointMake((currentPage * w) + ((w + a) / 2) + (offset.x * imageViewAmplifier), imageView.center.y);
}
}
I used the following calculation to determine the center position of each of the UIImageViews, and then add a portion of the offset to add the moving effect.
I managed to fix the issue by simplifying things - I have the tendency to overcomplicate everything which made this task a lot more difficult than it needed to be.
Instead of working out the current center of the imageView and performing the translation by transforming the center point, I used a simple CGAffineTransformMakeTranslation() which effectively deals with all of this for me. All I had to do from there was work out the actual offset from the origin of the page, and then fix the amplifier, which was corrected by working out the ration between the actual offset and the content size of the scroll view. My working solution can be found below:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
NSInteger currentPage = floorf(offset.x / CGRectGetWidth(self.view.frame));
if (currentPage >= 0 && currentPage < CINNumberOfWalkthroughs) {
self.pageControl.currentPage = currentPage;
UIImageView *imageView = self.imageViews[currentPage];
float multiplier = -1 * (offset.x / scrollView.contentSize.width);
float w = CGRectGetWidth(self.view.frame);
imageView.transform = CGAffineTransformMakeTranslation((offset.x - currentPage * w) * multiplier, 0);
UILabel *instructionLabel = self.instructionLabels[currentPage];
instructionLabel.transform = CGAffineTransformMakeTranslation((offset.x - currentPage * w) * (multiplier - 0.4), 0);
}
}

UIPanGestureRecognizer - Translations and rotations not happening evenly with negatives vs positives

Towards the top of the file I have this:
#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0)
In viewdidload I have this:
imageView.layer.anchorPoint = CGPointMake(0.5,0.5);
Then finally in the gesture recognizer I have this:
- (IBAction)handlePan:(UIPanGestureRecognizer *)sender {
CGPoint translation = [sender translationInView:self.view];
xPos += translation.x;
if(xPos > 150) xPos = 150;
if(xPos < -150) xPos = -150;
float rotate = 0;
if(xPos >= 50) {
rotate = (xPos - 50) * 0.025;
} else if(xPos <= -50) {
rotate = (xPos + 50) * 0.025;
}
imageView.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rotate)), (xPos * 0.35), 0);
}
This appears to be applying the exact same effects to negatives (left) and positives (right).
With the UIImageView that is being rotated being placed in the center of the screen horizontally, and its anchor set to the center I would expect the maximum in both directions to be cut off equally by the edge of the screen. Instead the image goes much further to the right.
Left effect (negative):
Right effect (positive):
It looks like autolayout changes the anchor point as the image view is rotated and the frame changes.

How to redraw a certain rect in drawRect

Here is my problem. I am planning to draw 999 circles inside a view at the time of setup, which is done successfully in drawRect. After that, I am planning to edit the color of each circle when user touches (which i am handling using the tap gesture in the view controller). The problem is, when I call [self.pegView setNeedsDisplay]; the drawRect is getting called and goes to the else part of the if in drawRect (which it should), but all the other beads are removed, and I see only 1 bead instead of 999 beads.
Below is my code.
- (void)drawRect:(CGRect)rect
{
if(!self.isEditingHappening) // For the initial setup
{
CGRect beadRect = CGRectMake(BEAD_ORIGIN_X, BEAD_ORIGIN_Y, BEAD_VIEW_SIZE, BEAD_VIEW_SIZE);
self.ctx = UIGraphicsGetCurrentContext();
for(int i = 0 ; i < 999 ; i++)
{
// To set up the bead view for each column
if (i !=0 && i % 37 !=0)
{
beadRect.origin = CGPointMake((beadRect.origin.x + BEAD_VIEW_SIZE), beadRect.origin.y);
}
// To set up bead view for each row
if (i !=0 && i %37 ==0 )
{
beadRect.origin.y += BEAD_VIEW_SIZE;
beadRect.origin = CGPointMake(BEAD_ORIGIN_X, beadRect.origin.y);
}
BeadPattern *pattern = [self.beadPatternsArray objectAtIndex:(i/37)];
int beadType=[[pattern.eachRowPattern objectAtIndex:i%37] intValue];
BeadColor *color=[self.beadColorsArray objectAtIndex:beadType];
CGPoint center;
center.x = beadRect.origin.x + beadRect.size.width / 2.0;
center.y = beadRect.origin.y + beadRect.size.height / 2.0;
CGRect newInnerRect;
newInnerRect.size.width = beadRect.size.width * 0.6;
newInnerRect.size.height = beadRect.size.height * 0.6;
newInnerRect.origin.x = center.x - newInnerRect.size.width/2;
newInnerRect.origin.y = center.y - newInnerRect.size.height/2;
[[UIColor whiteColor] set];
UIRectFill(beadRect);
// Adds the outer circle
CGContextAddEllipseInRect(self.ctx, beadRect);
// Fill the outer circle with any color
CGContextSetRGBFillColor(self.ctx, color.redValue, color.greenValue, color.blueValue, 1);
CGContextFillEllipseInRect(self.ctx, beadRect);
//Add inner circle
CGContextAddEllipseInRect(self.ctx, newInnerRect);
//Fill inner circle with white color
CGContextSetRGBFillColor(self.ctx, 1, 1, 1, 0.4);
CGContextFillEllipseInRect (self.ctx, newInnerRect);
}
}
else // When editing is happening
{
NSLog(#"The redrawing rect is %#", NSStringFromCGRect(rect));
CGContextSaveGState(self.ctx);
CGRect beadRect;
beadRect.size = CGSizeMake(BEAD_VIEW_SIZE, BEAD_VIEW_SIZE);
beadRect.origin = CGPointMake(self.columnToBeUpdated * BEAD_VIEW_SIZE, self.rowToBeUpdated * BEAD_VIEW_SIZE);
CGPoint center;
center.x = beadRect.origin.x + beadRect.size.width / 2.0;
center.y = beadRect.origin.y + beadRect.size.height / 2.0;
CGRect newInnerRect;
newInnerRect.size.width = beadRect.size.width * 0.6;
newInnerRect.size.height = beadRect.size.height * 0.6;
newInnerRect.origin.x = center.x - newInnerRect.size.width/2;
newInnerRect.origin.y = center.y - newInnerRect.size.height/2;
CGContextRestoreGState(self.ctx);
[[UIColor whiteColor] set];
UIRectFill(beadRect);
// Adds the outer circle
CGContextAddEllipseInRect(self.ctx, beadRect);
// Fill the outer circle with any color
CGContextSetRGBFillColor(self.ctx, self.colorToBeShownWhileEdited.redValue, self.colorToBeShownWhileEdited.greenValue, self.colorToBeShownWhileEdited.blueValue, 1);
CGContextFillEllipseInRect(self.ctx, beadRect);
//Add inner circle
CGContextAddEllipseInRect(self.ctx, newInnerRect);
//Fill inner circle with white color
CGContextSetRGBFillColor(self.ctx, 1, 1, 1, 0.4);
CGContextFillEllipseInRect (self.ctx, newInnerRect);
}
}
What I intend to do is, keep all the 999 beads, but edit only one bead's color for which the user has touched. I tries using [setNeedsDisplayInRect] also, but the rect is not passed correctly. Please help me here.
If I remember correctly, it clears off the other items on the canvas -so your initial setup stuff would need to run each time.
Or, you could create separate views for each bead that would allow you to just follow that one specifically. Each bead could contain its own logic.

UIScrollView with subView Zooming

I Want to implement a scrollview that has several UIViews inside of it. The left-most item needs to be larger than the rest of the items. So my problem is this. Whenever an item is leaving the screen to the left (its origin.x is less than 15), I need to scale the item down from 470x440 to 235x220 pixels. This is fairly simple to implement. The problem is that the item that is moved to the left of pixel 480 needs to be zoomed in from 235x220 pixels to 470x440 pixels AND it needs to be moved to the left by 235 pixels (so as to not cover the item to its right, but rather move into the space that the leaving element left when it "shrunk".
I have tried a few different approaches to this, but I cannot the the animation to look good, and there is a bunch of glitches here and there.
Does anyone have any idea how I might go about implementing this type of feature ? Note that I do not want to zoom, but I want to resize the elements inside the scroll view in such a way that the left-most element (that is visible on the screen) is double the size of the other elements.
In case anyone else might be interested, I ended up with the following inside scrollViewDidScroll:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
float contentOffset = scrollView.contentOffset.x;
int leavingElementIndex = [_scrollView indexOfElementLeavingScene:scrollView.contentOffset.x];
int entereingElementIndex = leavingElementIndex + 1;
if (leavingElementIndex >= 0 && contentOffset > 0) {
CGRect leavingFrame = [[[scrollView subviews] objectAtIndex:leavingElementIndex] frame];
CGRect enteringFrame = [[[scrollView subviews] objectAtIndex:entereingElementIndex] frame];
float scalePerentage = (contentOffset - (_scrollView.smallBoxWidth * leavingElementIndex))/(_scrollView.smallBoxWidth);
enteringFrame.size.width = _scrollView.smallBoxWidth + (_scrollView.smallBoxWidth * scalePerentage);
enteringFrame.size.height = _scrollView.smallBoxHeight + (_scrollView.smallBoxHeight * scalePerentage);
enteringFrame.origin.x = [_scrollView leftMostPointAt:entereingElementIndex] - (_scrollView.smallBoxWidth * scalePerentage);
[[[scrollView subviews] objectAtIndex:entereingElementIndex] setFrame:enteringFrame];
leavingFrame.size.width = _scrollView.largeBoxWidth - (_scrollView.smallBoxWidth * scalePerentage);
leavingFrame.size.height = _scrollView.largeBoxHeight - (_scrollView.smallBoxHeight * scalePerentage);
[[[scrollView subviews] objectAtIndex:leavingElementIndex] setFrame:leavingFrame];
//Reset the other visible frames sizes
int index = 0;
for (UIView *view in [scrollView subviews]) {
if([view isKindOfClass:[SlidingView class]] && index > entereingElementIndex) {
CGRect frame = view.frame;
frame.size.width = _scrollView.smallBoxWidth;
frame.size.height = _scrollView.smallBoxHeight;
frame.origin.x = [_scrollView leftMostPointAt:index];
[view setFrame:frame];
}
index++;
}
}
}
This is what it looks like in the end:
End Result http://stuff.haagen.name/iOS%20scroll%20resize.gif

Resources