UIScrollview paging like animation - iphone

I have a view that have some images, text, and more to show and two button in left and right when user click on left or righr button page should get side and another page should get open with new info.
How i can do it please help.

try with this link.. You will get the logic to solve your problem. Place a button on each side where you want & use the logic from this thread

Enable paging for the scrollview.
When a button is tapped modify the content offset of the scrollview by the width of the scrollview using setContentOffset:animated:

Use this code
-(IBAction)prevButtonPage:(id) sender
{
CGFloat xOffset = pagingScrollView.contentOffset.x;
CGFloat yOffset = pagingScrollView.contentOffset.y;
if (pagingScrollView.contentOffset.x != pagingScrollView.frame.origin.x)
{
[pagingScrollView setContentOffset:CGPointMake(xOffset- 320, yOffset) animated:YES];
// [arrayIndia objectAtIndex:i];
}
NSLog(#" custom x==%f %f", pagingScrollView.contentOffset.x, pagingScrollView.contentSize.width);
}
-(IBAction)nextButtonPage:(id) sender
{
CGFloat xOffset = pagingScrollView.contentOffset.x;
CGFloat yOffset = pagingScrollView.contentOffset.y;
if ((pagingScrollView.contentOffset.x != pagingScrollView.frame.origin.x)&&(pagingScrollView.contentOffset.x !=[imageArray count]) )
{
[pagingScrollView setContentOffset:CGPointMake(xOffset + 320, yOffset) animated:YES];
}
NSLog(#" custom x==%f %f", pagingScrollView.contentOffset.x, pagingScrollView.contentSize.width);
}

Related

Popovercontroller doesn't display full

I have an UIPopovercontroller in with an content that generate from an viewcontroller.
popOverController = [[UIPopoverController alloc]initWithPopUp:emLightPopUp];
popOverController.delegate = self;
// Get device position.
CGRect position = {parentButton.frame.origin,emLightPopUp.popUpView.frame.size};
CGSize popUpViewFrameSize = emLightPopUp.popUpView.frame.size;
Link screenshot: http://i.stack.imgur.com/AxqoG.png
The issue is that when I change the position of select devices (touched button) upwards, the displayed popup will be resized like the screenshot.
I already try to set content size inside the subclass of uipopover but it still doesn't work:
self.popoverContentSize = emLightPopUp.popUpView.frame.size;
EDIT:
I solved this problems by calculate the position that display the popup and scroll the scrollview inside into an upper position. Check out this code:
-(void)moveDeviceOutMiddleScreen:(id)deviceButton
{
UIButton* button = (UIButton*)deviceButton;
CGFloat yPositionRange = button.frame.origin.y - self.floorZoomScrollView.contentOffset.y;
int middle_top_y = 70;
int middle_bottom_y = 166;
if (yPositionRange > middle_top_y && yPositionRange < middle_bottom_y) {
CGRect newRect = CGRectMake(self.floorZoomScrollView.contentOffset.x,
self.floorZoomScrollView.contentOffset.y +yPositionRange*0.6,
self.floorZoomScrollView.frame.size.width, self.floorZoomScrollView.frame.size.height);
[self.floorZoomScrollView scrollRectToVisible:newRect animated:NO];
}
}
Thanks for your responses.
You are setting the value with itself.
self.popoverContentSize = emLightPopUp.popUpView.frame.size;
Instead, you should set the desired size (assuming emLightPopUp is the view controller displayed in the popup controller):
[self setPopoverContentSize:emLightPopUp.bounds animated:YES];

Scrolling an image continuosly

I need in my project to make the scroll an image continuously, ie from the bottom of the image once scrolled by continuing to scroll to the top should start from the base and so on, in a sort of loop, how can I do to do this?
Thanks
In this example i am taking total 5 images
- (void)viewDidLoad {
[super viewDidLoad];
// add the last image (image4) into the first position
[self addImageWithName:#"image4.jpg" atPosition:0];
// add all of the images to the scroll view
for (int i = 1; i < 5; i++) {
[self addImageWithName:[NSString stringWithFormat:#"image%i.jpg",i] atPosition:i];
}
// add the first image (image1) into the last position
[self addImageWithName:#"image1.jpg" atPosition:5];
scrollView.contentSize = CGSizeMake(320, 2496);
[scrollView scrollRectToVisible:CGRectMake(0,416,320,416) animated:NO];
}
- (void)addImageWithName:(NSString*)imageString atPosition:(int)position {
// add image to scroll view
UIImage *image = [UIImage imageNamed:imageString];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,position*416,320, 416);
[scrollView addSubview:imageView];
[imageView release];
}
implement delegate method
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
NSLog(#"%f",scrollView.contentOffset.y);
// The key is repositioning without animation
if (scrollView.contentOffset.y == 0) {
// user is scrolling to the left from image 1 to image 4
// reposition offset to show image 4 that is on the right in the scroll view
[scrollView scrollRectToVisible:CGRectMake(0,1664,320,416) animated:NO];
}
else if (scrollView.contentOffset.y == 2080) {
// user is scrolling to the right from image 4 to image 1
// reposition offset to show image 1 that is on the left in the scroll view
[scrollView scrollRectToVisible:CGRectMake(0,416,320,416) animated:NO];
}
}
I gives the two reference link for the automatically scroll the scroll view where the you can grap the source code also and implement into your project it's amazing funcationality
1) Automatically scroll the scroll view
2) As par your requirement created post of like wise gallery.
May this post is useful for you.
Happy codeing
Thanks and Regards,
#Samuel

UIScrollView- How far is it scrolled?

I'm using an UIScrollView and I have an image that indicates to the user that there is more content that they can scroll through vertically. I would like this image to be hidden when the scrollview is all the way at the bottom. Is there a way to do this? Would I have to subclass UIScrollView and make my own?
your scroll view's delegate should repsond to scrollViewDidEndScrollingAnimation: and use that to check where you are
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// Get some details about the view
CGSize size = [scrollView frame].size;
CGPoint offset = [scrollView contentOffset];
CGSize contentSize = [scrollView contentSize];
// Are we at the bottom?
if (-offset.y + size.height <= contentSize.height)
NSLog(#"bottom");
else
NSLog(#"not bottom");
}
NB The if statement was done in my head so it might be the wrong way round ;)

UIScrollView - with paging enabled, can I "change" the page width?

What's the simplest way to have a scroll view (with pagingEnabled set to YES) have a page width set to something other than the scroll view's bounds?
Let me give an example. Suppose I have a scroll view with 10 items, each 150 pixels wide, and my scroll view is 300 pixels wide. If I start with views 1 and 2 visible and scroll horizontally to the right, I want the next "page" to show items 2 and 3. If I scroll one more page to the right, I would see items 3 and 4.
Has anyone done this? If not, what strategy would you use?
Alexander Repty just blogged about this very topic: http://blog.proculo.de/archives/180-Paging-enabled-UIScrollView-With-Previews.html. Short answer: it's not as simple as just changing the width of the 'page', but it's not too hard.
Mike,
use the approach from my blog that Ben linked you to. Make the UIScrollView half the size of the surrounding view (i.e. 150px) and move it to the left instead of leaving it centered.
That should give you exactly the behaviour you wanted.
Cheers,
Alex
It can surely be done with the help of scrollview delegate methods & content-offset.
Below is the detailed code for the same.
int start;
int end;
int k=0;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView1
{
pageControlUsed = NO;
start = scrollView.contentOffset.x;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView1 willDecelerate:(BOOL)decelerate
{
end = scrollView.contentOffset.x;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView1{
int diff = end-start;
if (diff>0)
{
k=k+150;
[scrollView setContentOffset:CGPointMake(k, 0) animated:YES];
}
else {
k=k-150;
[scrollView setContentOffset:CGPointMake(k, 0) animated:YES];
}
}
Simply override scrollViewWillEndDragging
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
CGFloat pageWidth = 160;
targetContentOffset->x = pageWidth * (int)(targetContentOffset->x / pageWidth);
}
UIScrollView *album = [[UIScrollView alloc] init];
album.translatesAutoresizingMaskIntoConstraints = NO;
album.pagingEnabled = YES;
album.clipsToBounds = NO;
album.showsHorizontalScrollIndicator = NO;
album.contentInset = UIEdgeInsetsMake(0, -50, 0, -50);
[view addSubview:album];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-50-[_album(220)]" options:0 metrics:metrics views:views]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_album(300)]" options:0 metrics:metrics views:views]];
this works fine on iOS 7.1~ 8.2

Controlling AutoCorrect View Position on iPhone

Is there a way to control the position of the auto correct view that pops up while typing in a UITextField?
By default it appears to always appear below the text field. However in Apple's apps like SMS it sometimes appears above the text field.
For text fields aligned right above the keyboard the auto correct is blocked by the keyboard and not usable.
The position of the auto correction prompt is determined by the firstRect... method of the UITextInput protocol. Unfortunately UITextField uses a private class as delegate for this protocol so you cannot subclass and override it.
You COULD however implement your own UITextField replacement by drawing the contents yourself (like with CoreText), implement your own selection and loupe handling and then you would be able to affect the position of the auto correction prompt. Though it's designed to always be below the edited text, so you would have to essentially lie on the firstRect ... method.
Long story short: it's too much hassle.
One answer it worked for me is to use setInputAccessoryView method of the textview/textview.
I have a toolbar which contains the textView.
If I set the toolbar as the inputaccessoryview of the textfield, and set to NO clipsToBound property of the toolbar, I don't know exactly why, but the balloon appears above the keyboard
Here is a solution using private APIs as there are no ways to do this using public APIs.
Hunt through the application's -windows property to find the private UITextEffectsWindow window and figure out its frame. This is the keyboard
Hunt through the TextView's subviews to find the private UIAutocorrectInlinePrompt view. This is the autocorrect bubble.
Move that subview into a separate wrapper view (added to the TextView) and then move that wrapper view so it's above the above-mentioned keyboard window.
Using swizzled layoutSubViews,
- (void) moveSpellingCorrection {
for (UIView *view in self.subviews)
{
if ([[[view class] description] isEqualToString:#"UIAutocorrectInlinePrompt"])
{
UIView *correctionShadowView = nil; // [view correctionShadowView];
for (UIView *subview in view.subviews)
{
if ([[[subview class] description] isEqualToString:#"UIAutocorrectShadowView"])
{
correctionShadowView = subview;
break;
}
}
if (correctionShadowView)
{
UIView *typedTextView = nil; //[view typedTextView];
UIView *correctionView = nil; //[view correctionView];
for (UIView *subview in view.subviews)
{
if ([[[subview class] description] isEqualToString:#"UIAutocorrectTextView"])
{
if (CGRectContainsRect(correctionShadowView.frame,subview.frame))
{
correctionView = subview;
}
else
{
typedTextView = subview;
}
}
}
if (correctionView && typedTextView)
{
CGRect textRect = [typedTextView frame];
CGRect correctionRect = [correctionView frame];
if (textRect.origin.y < correctionRect.origin.y)
{
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0,-50.0);
[correctionView setTransform: moveUp];
[correctionShadowView setTransform: moveUp];
CGRect windowPos = [self convertRect: view.frame toView: nil ];
[view removeFromSuperview];
[self.window addSubview: view];
view.frame = windowPos;
}
}
}
}
}
}
For more details check.
In my case, the AutoCorrect view's position is shifted because of the font's leading (line gap).
So, I tried to move it up the leading px by using firstRect function as the code below.
class CustomTextView: UITextView {
override var font: UIFont? {
didSet {
if let font = font {
leadingFont = font.leading
} else {
leadingFont = 0
}
}
}
var leadingFont: CGFloat = 0
override func firstRect(for range: UITextRange) -> CGRect {
var newRect = super.firstRect(for: range)
newRect.origin = CGPoint(x: newRect.origin.x, y: newRect.origin.y - leadingFont)
print("newRect: \(newRect)")
return newRect
}
}
Although it's UITextView but you can do the same thing with UITextField