circular infinite scrollview in objective c - uiscrollview

I have added a button in a uiscrollview and 5 buttons visible in scrollview at a time. I want to make a circular effect in uiscrollview without paging following functionality are needed.
Circular scrolling for example: 5 1 2 3 4 5 1
Make a selection in uiscrollview like uipickerview when any button selected in scrollview. Then it goes in center:
For example--before selection: 1 2 3 4 5, when suppose select 5th button, then after selection: 3 4 5 1 2.
And when select left button from center the move the scrollview right, when right btn then move the scrollview left.
Suppose O scroll the scrollview when select button in mid of scrolling, then button event is not fired.

Related

auto layout messing up my buttons. What I am doing wrong? one button is taking up more space than the other despite all constraints being equal?

I have two buttons that have the same width and height. One Button is on the top left of the screen, while the other is on the top right of the screen. Here are the constraints:
left Button(colored Black):
Height 105, width 225,
align top to safe Area with 30 points,
align leading to safe area with 0 points.
Right Button(colored Blue):
height 105, width 225,
align top safe are with 30 points,
align trailing to safe area with 0 points.
shouldn't they be the same exact size? why is the blue button taking up more space in smaller screens?blue button takes up more space in smaller screens
Devices have different view widths...
A few examples:
iPhone 14 Plus 428
iPhone 14 Pro Max. 430
iPhone 14 Pro 393
iPhone 14 390
iPhone SE 3rd gen. 375
iPhone 13 390
iPhone 13 mini 375
You are setting each button width to 225 ... 225 + 225 = 450
So, the buttons are the same size, but they are overlapping.
If you want two equal-width buttons, constrain them like this:
blackButton.top to safeArea.top + 30
blueButton.top to safeArea.top + 30
blackButton.leading to safeArea.leading
blueButton.trailing to safeArea.trailing
and, the key constraint:
blueButton.width equalTo blackButton.width

Arranging cards: 2 cards on top. 1 card on the bottom

I have these three cards in a line across the bottom:
and I want them to be a triangle: 2 cards on top, 1 card on bottom. like this:
c
I tried "flex-wrap:wrap" already

Grid element stacking in fixed area with fixed width and height elements

I’m trying to figure out a solution of stacking or “packing” a series of rectangles and squares into blocks of 3x1 or 6x2 so that there isn’t a block with a protruding element and an uneven end. The width of the blocks can be a maximum of 3, before it has to break on another line. Image link below.
I have tried using 3 systems of measurements, Width + Height, Space (Num of single blocks an element occupies) + Width, Space + Height.
There are 5 possible elements that the grid can use. Image link below.
Using the S + H paradigm they are:
Name | Space x Height
Large - 4 x 2
Fullwidth - 3 x 1
Horizontal - 2 x 1
Vertical - 2 x 2
Item(Single) - 1 x 1
So far the logic has taken me to the situation that a block will always be either 3x1 or 6x2. If the spaces taken exceed 3, it means the block must be at least 2 blocks tall, e.g if there is 2 elements with a total of 4 spaces the remaining elements must occupy only 2 spaces.
I've excluded the possibility of 9x3 for simplicity.
Horizontal + Item
Horizontal = 2 x 1
2 spaces 1 Tall.
Doesn’t fill out a row so we need another element.
Item = 1 x 1
2 + 1 = 3 Spaces
1 = 1 = 1 Tall
Horizontal + Item = 3x1.
Item + Item + Item
1x1 = 1 Space 1 Tall
1x1 + 1x1 = 2 Spaces 1 Tall
1x1 + 1x1 + 1x1 = 3 Spaces 1 Tall
Item + Item + Item = 3x1.
Horizontal + Horizontal + Vertical
2x1 = 2 Space 1 Tall
2x1 + 2x1 = 4 Space 1 Tall
Spaces are > 3, therefore the block must be 6x2 in the end. There are 4 spaces which means there must be elements added so they occupy 2 more spaces and be 2 tall.
2x1 + 2x1 + 2x2 = 6 Spaces 2 Tall
I’m not sure if I’m close to a solution or way off.
The actual order of which elements are put in first doesn’t matter as I’m using an external library to place them in coherent blocks which handles that logic.
E.g Horizontal + Horizontal + Vertical == Horizontal + Vertical + Horizontal.
The logic will be written in PHP if that helps.
Image link here

Neighbours of a cell in the borders of a matrix

I'm facing some issues to find the neighbours in a matrix. I'm trying not to put a lot of if statements in the code because I'm pretty sure there's a better way to do it but I don't know exactly how.
To simplify, let's say we have the following matrix:
1 2 3 4 5
6 7 8 9 6
1 2 3 4 5
2 3 4 6 7
Considering the cell [2,2] = 3, the neighbours would be (i,j-1),(i-1,j),(i+1,j),(i,j+1),(i+1,j+1),(i-1,j-1). I created a "mask" for it using a for-loop like this, where inicio[0] is the i-coordinate of my current element (2 in the example) and inicio[1] is the j-coordinate (also 2 for element 3). Also, I'm considering the element must be in the center of the mask.
for(k=inicio[0]-1;k<inicio[0]+1;k++){
for(z=inicio[1]-1;z<inicio[1]+1;z++)
if(k!=0 || z!=0) //jump the current cell
However, I don't know how to treat the elements in the borders. If I want to find the neighbours of element [0,0] = 1 for example, considering the element must be in the middle of the mask like this:
x x x
x 1 2
x 6 7
How can I treat those X elements? I thought of initializing the borders on zero but I'm thinking this is not the proper way to do it. So if anyone can explain a better way to do it or an algorithm, I will be glad.

Optimal Movement in grid

There is a maze of size N*M, consisting of unit blocks. At the start Alice has K percentage of energy.
Now Alice start from 1 st row and move towards N th row. From the present block she can move to a block in the next row, which is either on right or on left side of the present block. On moving to a block in i th row j th column, her energy will reduce by C(i,j) percent if C(i,j) is greater then 0, else it will be recharged by C(i,j) percent.
For Example if she has 50 percent of energy, on moving to block with C(i,j) = 15, she will have 35 ( 50 -15 ) percent of the it remaining.
Now the task is to find out the status of the Alice energy in the end, if she moves optimally to save maximum energy.
Note : Her energy will not exceed more than 100 percent, and she will not move further if her energy goes down to 0 percent .
EXAMPLE : Let us suppose a grid of 4*4 as follow :
2 -2 2 -2
-2 2 -2 2
1 -1 1 -1
-1 1 -1 1
And if K= 10 meaning she has 10 percent energy at start. then after reaching 4th row she will be having 16 percent energy.One of the optimal move will be <1,2> -> <2,1> -> <3,2> -> <4,1>
So here answer is 16.
I originally said you could solve this from the bottom up, but doing the example by hand I am going to work from the top down, because of the constraint that you cannot proceed once your energy goes down to zero - it doesn't come into play here, but it looks much easier to deal with if you are working from the top down. The principal is much the same - work row by row and at each stage write down the best score possible travelling through each cell, using the answers from the previous row to work out the answers for the current row.
We start out with 10, and I assume you can start out anywhere you want, so just subtract the top row from 10 to work out the best you can get to in the top row, including the energy difference from the cell you are now on. The top row becomes 8, 12, 8, 12.
The edge cells on the next row can only be reached in one way. The inner cells can be reached by two ways. In either case we work out the total there by taking account of the energy difference from that cell and the energy you could have from the cell you came from, taking the most promising choice when there are two. So we get 14, 10, 14, 10, where for example the second 14 is one of the 12ths above and to its left or above and two its right (if the scores were different we could take the best) combined with the -2 for that cell.
Similarly, we have 9, 15, 9, 15 for the next row and 16, 8, 16, 8 for the bottom, where for example the only way to get to the bottom left is from the 15 above and to its right, so adjusted by -1 we turn 15 into 16. There are two ways to get to the cell just to its right, but they both start from, 9, so adjusted by 1 we get 8.
If you need to whole path you can keep track of how you entered each cell as you work out the best cost to it, and then track your way up from the best answer when you are finished.

Resources