So i was trying yo create a simple auto scrolling view with iCarousel.
i made 4 view with image in the centre to be visible,
using the
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView
and log the index view that are currently rendered,
i test it on ipod touch / iphone 5 / iphone 6, and every view get rendered as the log printed
current index rendered 0
current index rendered 3
current index rendered 2
current index rendered 1
but when i try it on iphone 6+, the log just print :
current index rendered 0
current index rendered 3
current index rendered 1
is it just the carousel? i doubt it.. thats why, what should i do to make the carousel rendered the 3rd view (2)?
because on func numberOfItemsInCarousel(carousel: iCarousel) -> Int, i return the count of my image array.
thanks
Sorry, it seems it was the natural behaviour of the carousel of the reused view.
when it was on 5 or 6, the dimension of the view allow it to be all rendered when initialized, so the reuse view is not needed.
but when it was on 6+, the dimension is larger and it makes the carousel to render just 3 item : the current, the front/right and the back/left (if wrap is enabled, and my setting is enabled)
then i came to realize that im doing it wrong when reuse the view.
what i did before was to get the UI components then recreate their rect.
while the example didn't do that.
so my solution was to not recreate the rect and just change the image attribute of the UIViewImage of the subview
Related
I have a ScrollableRow with lets say 5 fullscreen composables.
ScrollableRow(
modifier = Modifier.fillMaxSize(),
scrollState = scrollState) {
myData.items.forEachIndexed{ index, pageItem ->
showPage(pageItem)
}
}
Everything is shown so far, but when I swipe left/right it stops exactly on the position, where I stopped swiping:
As you can see and imagne, almost everytime it stops between two of these full screen composables.
I'd rather have a kind of stickyness and show full pages for each single left/right swipe as pictured here:
So obviously, here in the second picture with third page showing:
when I swipe one time left, I want to see full screen page 4, only
when I swipe one time right, I want to see full screen page 2, only
I bet there is a mechanism for this?
HorizontalPager will suit you. Check Accompanist Pager layouts
I started looking at Paul Hudson's Hacking with Swift tutorial on positioning views in a grid. If I run the code given in the first listing, I get exactly what's shown in the corresponding screenshot. In other words, this code ...
import SwiftUI
struct ContentView: View {
let data = (1...100).map { "Item \($0)" }
let columns = [
GridItem(.adaptive(minimum: 80))
]
var body: some View {
ScrollView {
LazyVGrid(columns: columns, spacing: 20) {
ForEach(data, id: \.self) { item in
Text(item)
}
}
.padding(.horizontal)
}
.frame(maxHeight: 300)
}
}
... ran on an iPad 11 Pro Max simulator gets me the following:
So far so good.
But I want to show a grid of Text views with a bit more content, so I change the first line of my view to ...
let data = (1...100).map { "Item \($0) - more content" }
... but this gets me ...
This is where my understanding falls apart. I would like to see a grid where each Text view is given the amount of horizontal space needed to accommodate the longest content, without line breaks or truncation. Something like this:
Item 1 - more content Item 2 - more content
Item 3 - more content Item 4 - more content
...
Item 42 - more content Item 43 - more content
Item 44 - more content Item 45 - more content
...
Item 142 - more content Item 143 - more content
Item 144 - more content Item 145 - more content
...
Item 7142 - more content Item 7143 - more content
Item 7144 - more content Item 7145 - more content
...
And if my data was long enough that strings were longer than half the width of the screen, I'd expect the grid to utilize just a single column.
Why don't the Text views take up more horizontal space? We've only specified a minimum for our adaptive GridItems, so why would they not expand horizontally as needed to accommodate the longest string? The documentation for GridItem.Size.adaptive says (emphasis mine):
This approach prefers to insert as many items of the minimum size as possible but lets them increase to the maximum size.
So how would I get the grid to let these views increase to their maximum size (which is .infinity by default)?
I've tried adding .fixedSize() to each Text, hoping it would force them to retain their ideal size. But this does not work:
Similarly, I've tried .lineLimit(1) as well, but this results in truncation:
I'm aware of three similar questions (here, here, here) but they are trying to achieve a variable line length with the given data. In my case I really do want a grid. I just want each grid item to take up as much horizontal space as the largest one. How could I achieve this?
I feel like I'm missing something simple about the way SwiftUI layout (or that of a grid) works. I know that during layout the parent view proposes a size to its children, and each child responds with its desired size. In this case it looks like the grid has already decided it will fit four columns on the screen, so it only offers roughly a quarter of its width to each Text. I don't follow why that would be the case when we've asked the grid items to be adaptive and only provided a minimum width.
In a "teach a man to fish ..." fashion, I'd greatly appreciate any advice or tooling that would help me understand how you arrive at your specific answer. For instance, is there a way for a developer to follow (debug print perhaps?) the proposals and counterproposals between parent and child views? Is there any documentation, explanation, etc. to learn more about what exactly goes on during the layout pass? Any detailed explanation of how exactly a grid decides on the sizes of adaptive GridItems?
I have added a UIWebview to a view controller, can show a working html page that has draggable elements and a lot of javascript. The buttons work, links work, images load, javascript runs, all seems well. But the draggable elements don't drag. Instead the webview just scrolls within the view with bounces. A long tap just selects the whole web view.
How to I enable this web page to do a drag and drop instead of just scrolling it within the UIView?
If you load your HTML in Mobile Safari, does it also scroll the view rather than dragging elements? If so, you'll need to change your JavaScript so that it's compatible with touch-based dragging.
See: Javascript Drag and drop for touch devices
White boxes represent items in an NSArray.
Swiping from a directing changes the indexes and update the items accordingly. (not scrolling 1 by 1 but like changing pages. So, changing all 4 items at every swipe)
In this example, there are approximately 20 items in this array and we are in the second page. It currently shows item at index 4,5,6,7.
There can be more items.
I don't want to create at least 20 items and keep them at a far frame (CGRect).
I want to have a scheme like UITableView, where it creates approximately 6 items for infinite number of items if the tableview can show only 4 items at a time.
So, I plan to have 5 or 6 items for 6+ items and just update the item's contents (item = white boxes) according to real data at that index of NSArray
Is there a quick and ready way to do this easily or may there be a better way to do something like this?
instead of using tableview you can use scrollview with page control.
try using this link :https://github.com/cwalcott/UIScrollView-Paging/tree/buttons
I have a scrollView with 4 page; I want to change it's background color at every page; then I want to know index page value. For example, if I have page 1, I want to know that I have index 1 then background must be red or if I have page 3, I want to know that I have index 3 and background color must be blue.
then I want to do an "if" where I control if my current page have index 1, or index 2, or index 3.....
The page index is found by: scrollview.contentOffset.x / scrollview.frame.size.width.
To set the background color you can either:
implement the delegate methods for responding to scroll ending.
Insert subviews with their background colours set at the page index offsets.
There is problem with option one. When transitioning between pages the background colour will be incorrect until the scrolling ends.