make a button that moves image - ios

I want to make a button that moves my image one frame height up and if the button is pushed again move the image one frame height back down.
http://imageshack.us/photo/my-images/140/72197925.png/
(Illustration of the solution)
I am trying to use as little code as possible.
UIImage *image = [UIImage imageNamed:#"01bear.png"];
imageView = [[UIImageView alloc] initWithImage:image];
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:applicationFrame];
scrollView.contentSize = CGSizeMake(5760, 1);
[scrollView addSubview:imageView];
[window addSubview:scrollView];
[window makeKeyAndVisible];
scrollView.delegate = self;
scrollView.pagingEnabled = YES;

on button's touchUpInside event you need to change the:
scrollView.contentOffset = CGPointMake(X,Y);//required value to scroll the scrollview
if you only want the imageview to move, you can use:
[imageView setCenter:CGPointMake(X,Y)];//required value

Some questions:
Are you using the scroll view for other things, or just to get your image to move?
Do you want your image view to animate up, then down, or move suddenly?
A scroll view is awfully complex if all you want to do is have your view animate up, then down. To do that, you should look at the UIView class method animateWithDuration:animations: and it's variations.

Related

UIScrollView scroll functionality not working?

I am a newbie to IOS app development. I am trying to implement a scrollview. I am not able to scroll at all.
Below is my view implementation.
I created a scroll view and added small blocks with different colors in it.
I added one block out of current screen bounds so that I can scroll down the UIScrollView and view it.
#implementation CustomView
- (instancetype)init
{
self = [super init];
if (self) {
CGRect screenRect = CGRectMake(70, 100, 250, 800);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
scrollView.scrollEnabled = YES;
[scrollView setBackgroundColor:[UIColor yellowColor]];
[scrollView setAlpha:0.2];
[self addSubview:scrollView];
CGRect temp = CGRectMake(100, 150, 10, 10);
UIView *firstView = [[UIView alloc] initWithFrame:temp];
[firstView setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:firstView];
temp = CGRectMake(150, 200, 10, 10);
UIView *secondView = [[UIView alloc] initWithFrame:temp];
[secondView setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:secondView];
temp = CGRectMake(200, 750, 10, 10);
UIView *thirdView = [[UIView alloc] initWithFrame:temp];
[thirdView setBackgroundColor:[UIColor orangeColor]];
[scrollView addSubview:thirdView];
scrollView.contentSize = screenRect.size;
}
return self;
}
#end
I am using the view I created before in my view controller. I am adding it to my view controller's view.
#interface ViewController ()
#property (nonatomic, readonly) CustomView *currentView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_currentView = [[CustomView alloc] init];
[self.view addSubview:_currentView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am not able to scroll at all with this implementation. am i missing something ?
Can someone please explain what is wrong with my code.
Thank you
When you add a scrollview to the screen it should be the size which you want to display on the screen. It will be displayed on the screen simply as an UIView only when you look at it.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(20, 20, 250, 250)];
The above initialisation will give you a scroll view of the size 250X250 placed at the position (20,20).
For scrolling you need to define the content size of the scroll view.
This content size should be bigger than your scrollview size otherwise you will not be able to differentiate it. The size of the contentsize should be decided based on the content you are going to put inside it.
For example if the three views you are adding inside the scrollview occupy a total width of 500 and a height of 300 then your content size should be equal to that only.
[scrollView setContentSize:CGSizeMake(500, 300)];
This will allow you to scroll inside the scrollview.
Also the content you are adding to you scrollview should be added considering the top left corner of the scrollView as the (0,0) location.
So if you want to display something on the top left corner of your scrollView then the frame dimensions of that particular view should be like this
CGRectMake(0,0,30,30);
This will add a view of the size 30X30 on the top left corner of the scrollView.
Hope it helps.
Your scroll view doesn't scroll because its frame is as large as its content size. Add this line in the view controller after before adding the scrollview as subview:
_currentView.frame = self.view.bounds ;
The contentSize property should not be the size of the screen, it needs to be larger if you want scrolling to work. You should calculate the size of the content within the scrollView, for example the combined height of all the subviews and the spaces between them or maybe the MaxY of the bottom view.
Also, try using a UITableView (or a UIStackView in iOS 9) to accomplish the same behavior without doing any math
First of all, what you are trying to do should be done using UITableView. I am not sure why are you trying to do it this way.
Anyhow, did you try to set also the contentSize property of UIScrollView?
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/#//apple_ref/occ/instp/UIScrollView/contentSize

Change width and add a circular scroller to a UIScrollView

I am trying to increase the width of the scrollbar and add a circular scroller. Shall I use an image for the circle? I do not see any property or method of UIScrollView to change the width of the scroller
I tried the following:
UIScrollView *myScroll = [[UIScrollView alloc] init];
myScroll.frame = self.view.bounds; //scroll view occupies full parent view!
//specify CGRect bounds in place of self.view.bounds to make it as a portion of parent view!
myScroll.contentSize = CGSizeMake(400, 800); //scroll view size
myScroll.backgroundColor = [UIColor grayColor];
myScroll.showsVerticalScrollIndicator = YES; // to hide scroll indicators!
myScroll.showsHorizontalScrollIndicator = YES; //by default, it shows!
myScroll.scrollEnabled = YES; //say "NO" to disable scroll
[myScroll setBackgroundColor:[UIColor redColor]];
myScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[self.view addSubview:myScroll];
This custom ScrollView Solved all my problems https://github.com/BasheerSience/BRScrollBar

UIScrollView starts automatically in double size

I have an image that I want to display fullscreen in my app using UIScrollView and UIImageView, the image size is 640 x 2754 and I want to show it automatically in full screen mode in minimum zoom.
This is the code:
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"EquationPage.png"]]; self.imageView = tempImageView;
[tempImageView release];
scrollView.maximumZoomScale = 3.0;
scrollView.minimumZoomScale = 0.5;
self.scrollView.contentSize=CGSizeMake(640,2754);
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
To do this, you will have to use the setZoomScale:animated: method of UIScrollView. Confusingly, this method will not work unless you have a (non-nil) delegate set for your scroll view, which responds to -viewForZoomingInScrollView. Read the UIScrollView documentation for how to do this. For more info, you can also read this section about handling zooming with a UIScrollView.

iOS adding a top bar as a subview

I am currently working on an app for iPhone where I have an Imageview showing off some images and via swipe I can change the images.
But I'm currently trying to add a subview that will be a top bar, and when the image is touched, it will make this top bar slide down from the top.
The current code I've tried to do this with is:
CGRect frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 60.0f);
UIView *topBar = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:topBar];
[self.view bringSubviewToFront:topBar];
But the topbar won't show up, and I have logs showing that the touches are recognized.
How do I do this, and how do I properly populate the subview with buttons?
Best Regards.
FreeSirenety
Your topBar has nothing in it, so how can you know it has been added?
CGRect frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 60.0f);
UIView *topBar = [[UIView alloc] initWithFrame:frame];
topBar.backgroundColor = [UIColor redColor];
[self.view addSubview:topBar];
Also you don't need to use the bringSubViewToFront as the view that's gets added as a subview is always the topMost in the view hierarchy!

UIView in UIScrollView does not appear

I'm trying to create a horizontally scrollable menu similar to that in this video.
For some reason the UIView doesn't appear after adding a bunch of UIButtons to it and adding it to the UIScrollView. Here's my code (it's called in -viewDidLoad of a subclass of UIViewController):
//set up scrollview
UIScrollView *designPicker = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
//set up a view to drop into the scroll view
UIView * buttonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 431, 640, 49)];
//add buttons to scrollview
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
float runningX = designPicker.frame.origin.x;
for (i = 1; i <= 10; i++)
{
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tempBtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
CGRect rect = CGRectMake(runningX, designPicker.frame.origin.y, 30.0, 30.0);
tempBtn.frame = rect;
[buttonsView addSubview:tempBtn];
runningX = runningX + 35;
[tempBtn release];
}
[designPicker setContentSize:buttonsView.frame.size];
[designPicker addSubview:buttonsView];
[self.view addSubview:designPicker];
You should not add the buttons using the frame of the UIScrollView. The origin of the frame is in the superview's (superview of the UIScrollView) coordinates. You should make the buttons' frame relative to the UIView. So if you want the buttons to appear at the top of the view that you should start at (0,0).
Instead of adding scrollview as subview to your view, add view as subview of scrollview. As-
[self.scrollView addSubview:self.view];
// release scrollView as self.view retains it
self.view=self.scrollView;
[self.scrollView release];
And make sure your view should have large content size than content size of your scrollview.It worked for me.

Resources