I have a scrollview with 5 image views of width 88.
I want the scrollview to scroll to each image view (Not Paging)
and
I want to make an infinite scrollview in iPhone which means the when it scroll to last item it will display a first item next to it..
I have been trying by offset but it stops when move it by offset and also I have used apple street scroller which does not allow me to stop each element in center(just like Picker view)..
First of all, I recommend to use a UITableView so you can maintain the memory usage low. An approach that I had used successfully in a project is the following:
1. List item
Duplicate the content of your cells, I mean if you have 5 items in this way:
| 1 | 2 | 3 | 4 | 5 |
You should add the same (In a table view with reusable engine that's not a big deal) at the end of the table in order to look like this:
| 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 |
2. modify the scrollViewDidScroll with the following:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == _yourScrollView) {
CGFloat currentOffsetX = scrollView.contentOffset.x;
CGFloat currentOffSetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height;
if (currentOffSetY < (contentHeight / 6.0f)) {
scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2)));
}
if (currentOffSetY > ((contentHeight * 4)/ 6.0f)) {
scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2)));
}
}
}
The code above move the scroll position at top if you almost reach the final of the scrolling; Or if you are almost on the top, moves you to the bottom...
3. That's it.
You can have a look at one of this two options:
If you are registered as a developer, watch the session Advanced ScrollView Techniques from WWDC 2011.
A great tutorial from http://mobiledevelopertips.com/user-interface/creating-circular-and-infinite-uiscrollviews.html
This caused me serious problems for a long time so I really feel your pain. I found this control which should solve your problem: http://www.cocoacontrols.com/platforms/ios/controls/infinitescrollview
I also considered inserting transitions and disabled scroll so it becomes like a slideshow but either way should work.
Here is a short explanation of solution I used to do it.
Lets say you have counter from 0 - 9, then, when you make transition 9 -> 0, you actually spoof another 0 (call it 0') after 9 (in scrollView), make animated transition to 0' and than make non-animated instant transition to 0 that stands on top of scrollView.
You can use iCarousel library for this.
https://github.com/nicklockwood/iCarousel
For anybody else looking for a solution, you can also try this library Infinite Scroll Picker at https://github.com/Seitk/InfiniteScrollPicker/ It's basically drag and drop. I'm using it on a project and it works great.
Implemented a simple infinity scroll in this mini project:
https://github.com/mcmatan/Infinity-tabBar-scroll-view/blob/master/README.md
Related
I cannot seem to find a way of how to position all the buttons to the right. They are positioned on the left side by default. I have buttons for 1 day | 1 week | 2 weeks | 1 month but I need them to be on the top right side of the chart/page
Can you please help?
Unfortunaltey this option is not available, but you can vote for it here
In the next relase (nowadays beta) you will have chance to use buttonPosition.
Example: http://jsfiddle.net/fn90bye0/5/
buttonPosition: {
x: 110,
y: 117
},
I'm trying to write a little app, where on the main screen, I animate a flying "bubble". This animation has to be continuous. (I reuse the bubbles, which fly off the screen) I heard that animations have to run on the main thread, as does every operation which changes the UI. Is this true? When I try to show a UIAlertView on this screen, it's animation becomes very discursive because of the continuous bubble animation. (this is a custom alertview with an indicator) The device is an iPhone 4, so I don't think it should be a problem to show a normal UIAlertView.
And I would like to ask if I use the correct method for the bubble animation. So first of all, I use an NSTimer, which invokes the startAnimation method in every 0.01 seconds (I start it in the controller's viewDidAppear: method). In the startAnimation method, at first I generate bubbles with random x and y coordinates (to see bubbles on the screen right after the viewdidappear), and I generate bubbles on the bottom with random x and y = 460 coordinates. In the startAnimation method, I run a counter (called frames), and when the value of this counter equals 35, I call the bubble generate method again.
The problem:
I store the generated bubbles in an array, and the 'gone' bubbles (which are off the screen) in another array. First I try to reuse the bubbles in the gonebubbles array, then if the array is run out, I generate new bubbles. While this operation is processed, the continuous animation stops, then continues. The break is about one second, but this is very disturbing.
Can anyone help in this problem? Thanks in advice, madik
- (void)viewDidAppear {
.
timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(startAnimation) userInfo:nil repeats:YES];
.
}
- (void)startAnimation {
self.current = [NSDate timeIntervalSinceReferenceDate];
double diff = (self.start - self.current);
if ( diff < 0 ) {
diff = (-1) * diff;
}
self.start = self.current;
frames++;
if ( shouldMoveBubbles ) {
[mug moveBubbles:diff];
}
if ( frames == 35 ) {
DebugLog(#"################################################################");
DebugLog(#"####################### FRAME = 35 ###########################");
DebugLog(#"################################################################");
[mug createNewBubbleOnTheBottomOfView:self.view];
frames = 0;
}
}
In the Mug class:
- (void)moveBubbles:(double)millisElapsed {
for (Bubble *bubble in bubbles) {
int bubbleSpeed = bubble.speed;
float deltaX = (float)(bubbleSpeed * -degrees_sinus * millisElapsed * 100);
float deltaY = (float)(bubbleSpeed * -degrees_cosinus * millisElapsed);
DebugLog(#"movebubbles x: %f, y:%f, speed: %d, sin:%f, cos:%f", deltaX, deltaY, bubbleSpeed, degrees_sinus, degrees_cosinus);
[bubble moveBubbleX:deltaX Y:deltaY];
}
}
And in the Bubble class:
- (void)moveBubbleX:(float)deltaX Y:(float)deltaY {
self.bubbleImage.center = CGPointMake(self.bubbleImage.center.x + deltaX, self.bubbleImage.center.y + deltaY);
}
This sounds like a memory problem. Slow UIAlertView animation is a sure sign of this. It sounds like the way you are generating bubbles is causing the problem. You mentioned the you keep two arrays of bubbles. You never say if you limit the number of bubbles that can be in either array at once. You also don't mention when you clean up these bubbles. It sounds like a memory "black hole". I'd recommend setting a maximum number of bubbles that you can show on screen at once.
Also, you mention a custom alert view. If you're modifying the UIAlertView, you're going to run into problems since that's not officially supported. Additionally, I've seen UIAlertView animation become slow when memory is tight. If you solve the memory issues with your bubbles, you'll probably solve this one too.
Finally, a word of advice. Making an animated game in UIKit is probably not a good idea. NSTimers are not as accurate as many people would like to think. UIImages are relatively expensive to load. Touching moving buttons is known to be unreliable at worst, hackish at best. I suggest looking into a game framework, such as Cocos2d-iphone.
Good luck!
- (double) tableSize
{
double tableSize1=[self tableView:tableViewA numberOfRowsInSection:0]*tableViewA.rowHeight;
return tableSize1;
}
I use that code to know tablesize, but I want to know easier way to know it.. like tableviewA.size or something like that..
example : tableviewA have 20 tableviewcell, I want to know the tableviewA size.. how to know it?
If I do tableViewA.frame.size.height it'll always show 400, which is the height of the table frame, because that's the size of the table view. I want the size of the whole table including those I need to scroll. –
For example, if the tableviewA has lots of rows then I want to see a high number
You can use this - tableView.contentSize.height
this help?
tableView.frame.size.height
or
tableView.frame.size.width
then may be you can use
CGSize tableViewContentSize = tableView.contentSize;
to get the content size.
then to get the height and width just
tableViewContentSize.height
tableViewContentSize.width
Is it possible to keep a view on top of a another view while Core Animation is running on it?
I am sliding views in and out from the top and bottom. Currently, the view that is sliding out slides over the top of every other view that is currently visible, I'd like to know if I can make a view stay on top of the animation effects.
Here is an example of one of the animations I have now:
CATransition *push = [CATransition animation];
push.type = kCATransitionPush;
push.subtype = kCATransitionFromTop;
[self.grid.layer addAnimation:push forKey:kCATransition];
// Changes to the view here
[CATransaction commit];
The view I want to keep visible does not overlap the original position of the view that is sliding out.
This is an ASCII diagram of the layout of the screen:
+------------------------------+
| View I want to keep on top |
|------------------------------|
| |
|------------------------------|
| View that will slide up |
| ^ ^ ^ ^ ^ ^ |
| |
| |
| |
| |
| |
| |
+------------------------------+
After a bunch of playing around, I've realised that CoreAnimation respects the natural stacking order of UIViews. This means that if a view was added after the one being animated, it will be placed on top and the animation will happen behind it.
To alter the stacking order of the views, I can use the following messages:
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
The stacking order functions and behaviours of UIViews are detailed in other questions and answers.
I'm working on my first video game for the Android platform as a bit of a nights and weekends project.
It is coming along nicely, but I am very unhappy with the control sensativity.
In this game, you move an object left and right on the screen. On the bottom of the screen is a "touchpad" of sorts, which is where your finger should rest.
/-------------------------\
| |
| |
| |
| Game Area |
| |
| |
| |
| |
| |
/-------------------------\
| |
| Touch Area |
| |
\-------------------------/
I am currently using a state variable to hold "MOVING_LEFT, MOVING_RIGHT, NOT_MOVING" and am updating the location of the player object each frame based on that variable.
However, my code that reads the touchscreen input and sets this state variable is either too sensative, or too laggy, depending on how I tweak it:
public void doTouch (MotionEvent e) {
int action = e.getAction();
if (action == MotionEvent.ACTION_DOWN) {
this.mTouchX = (int)e.getX();
this.mTouchY = (int)e.getY();
}
else if (action == MotionEvent.ACTION_MOVE) {
if ((int)e.getX() >= this.mTouchX) {
this.mTouchX = (int)e.getX();
this.mTouchY = (int)e.getY();
if (this.TouchRect.contains(this.mTouchX, this.mTouchY)) {
this.mTouchDirection = MOVING_RIGHT;
}
}
else if ((int)e.getX() <= this.mTouchX) {
this.mTouchX = (int)e.getX();
this.mTouchY = (int)e.getY();
if (this.TouchRect.contains(this.mTouchX, this.mTouchY)) {
this.mTouchDirection = MOVING_LEFT;
}
}
else {
this.mTouchDirection = NOT_MOVING;
}
}
else if (action == MotionEvent.ACTION_UP) {
this.mTouchDirection = NOT_MOVING;
}
}
The idea is that when there is any movement, I check the previous location of the users finger and then figure out what direction to move the player.
This doesn't work very well, I figure there are some IPhone/Android developers on here who have figured out how to do good controls via a touchscreen and can give some advice.
You could try something similar to "drag rectangles" on Windows. When you hold down the mouse button on something, you don't start a drag operation until the mouse moves outside a small region around the mouse-down location. Reason being that it's very hard to keep the cursor on the same pixel while clicking.
So a first attempt could be (int)e.getX() >= this.mTouchX + DEAD_ZONE and similar for the other case, where DEAD_ZONE is a small integer.
This does not deal with turning around in the same stroke, however. You could deal with that by only turning left when the current position is at least DEAD_ZONE pixels to the left of the last position after turning right, and vice versa.
One obvious problem is that nowhere do you take account of the time since the last touch.
I would suggest you treat the players touch as an expression of desired movement on an analogue range from -x/+x and -y/+y, then perform the actual movement elsewhere based on time.
E.g.
objectPos += objectPos + (joyPos * timeDelta * maxSpeed);
So if the max-speed of your object is 10ms-1 and the players finger is at 0.5 on the control pad then the object would be moving 5 meters every second. If your game is running at 10fps then each frame the object would move 0.5 meters.
These figures are fictional, but hopefully demonstrate the need to separate control from movement and then factor in time.