I have list of items and this list have Scroll bar , in windows scroll bar work nice but in android touch screen scroll bar be very tin and user cant touch scroll bar , I want users can scrolling list with touch on list only .
TNX .
I find out.Must get touch screen pos and set a range for move .
scrollPosition1 = GUI.BeginScrollView(Rect (0,400,Screen.width,175),scrollPosition1, Rect (0, 0, 650, 0));
// touch screen
if (Input.touchCount==1 &&Screen.height -Input.GetTouch(0).position.y > 450 - scrollPositionHome.y && Screen.height - Input.GetTouch(0).position.y < 600 - scrollPositionHome.y )
{
var touchDelta2 : Vector2 = Input.GetTouch(0).deltaPosition;
scrollPosition1.x +=touchDelta2.x;
}
GUI.skin.font = fnt;
style.normal.textColor = Color.black;
style.alignment = TextAnchor.MiddleRight;
for (i=0;i < ImgSliderProducts.Length;i++)
{
GUI.DrawTexture(Rect(20+(i* 100),10,100,100), ImgSliderProducts[i],ScaleMode.ScaleToFit,true);
}
GUI.EndScrollView();
Related
We are trying to detect collision on a image view that is moving across the screen. The problem is we only want a negative reaction when the bottom or left side of the brickRect collides with the top or right of the ballRect. If the ballRect wants to land on the top of the brickRect then NO negative reaction is desired. Our question is how to write the if statements for these three interactions of the ballRect with the brickRect? We understand how to obtain the top bottom and left values We will post that code not sure it adds to the question
Rect brickRect = new Rect();
brickFOUR.getHitRect(brickRect);
int BRICKbottom = brickRect.bottom;
int BRICKtop = brickRect.top;
int BRICKleft = brickRect.left;
int BRICKright = brickRect.right;
The object here is to have the ballRect jump up and land on the brickRect
If the ballRect jumps and hits the bottom of the brickRect then do what ever
If the ballRect.right intercepts the brickRect.left then do what ever
So how do we turn this logic into code?
We have looked at numerous post but they all deal with over all detection
Assuming that you have a pair of positions or Rect models, for the before and after movement of the ball and that the position increases to the top and right, then you could write the code as follows:
Rect ballBefore = ...
Rect ballAfter = ...
Rect brickRect = new Rect();
brickFOUR.getHitRect(brickRect);
if(ballBefore.top > ballAfter.top
&& ballBefore.top < brickRect.bottom
&& ballAfter.top >= brickRect.bottom) {
//movement in top direction
//and position before is below
//and position after is not
}
if(ballBefore.right > ballAfter.right
&& ballBefore.right < brickRect.left
&& ballAfter.right >= brickRect.left) {
//if movement is in right direction
//and position before is left
//and position after is not
}
I downloaded the CNN iOS app and I seen the wonderful menu!
I post 2 screen
Now I'm asking how can I create that in my application. I will develop in iOS and Android but the same application for Android hasn't that menu..
It's possible to create this menu in Andorid?
Has anyone some advice to create that?
Thank you in advance
Got solution: Create one view with listiview, and one button with background photo. Than take the view out of screen. If the view is x = 320 y = 300 set y = -300 in sotryboard element postition settings and place the button in the top corner.
On button action make a animation of the 2 graphic object of y = +300. That's all.
- (IBAction)makeTheMagicWithButton:(id)sender {
//at first click y + 300 and view comes in
//at the 2nd click y - 300 and return back out the screen
float y;
[sender setSelected:![sender isSelected]];
if ([sender isSelected]) {
y = +300;
self.buttonImage = [UIImage imageNamed:#"menuICONSreturn.png"];
} else {
y = -300;
self.buttonImage = [UIImage imageNamed:#"menuICONS.png"];
}
//animation move the view
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionAllowAnimatedContent animations:^{[self.magicView setFrame:CGRectOffset(self.magicView.frame, 0, y)];}
completion:nil];
//animation move button menu
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionAllowAnimatedContent animations:^{[self.magicButton setFrame:CGRectOffset(self.magicButton.frame, 0, y)];}
completion:nil];
//change icon
[self.magicButton setBackgroundImage:self.buttonImage forState:UIControlStateNormal];
}
Hope it will help someone..
I am trying to allow the user to drag and drop and image from on position to another. The screen layout is as follows:
1 2 3
4 5 6
7 8 9
I want the user to grab image 2, 4, 6, or 8 and drag it to image 5. Upon dragging to image 5 I want to load up a fragment. The user can only drag the image in a straight line from it's current position to 5's position. ie image 2 and only drag down and only until it is overtop of image 5, image 4 can only drag right until overtop of 5, etc.
Any insight on how to do this is greatly appreciated.
Thanks,
DMan
So I figured out a way to get it to work. Pretty hacky but I basically use the image positions and transition it via a margin in the direct that I want from it's center until it reaches the desired position.
I created a temp image (mTempImage) and hide my main image because the image had parameters set in the layout that forced it to stay above a specific item in the layout and wouldnt let it margin less than that items height.
Here is a sample from my onTouchListener
int eventX = (int)event.getX();
int eventY = (int)event.getY();
int movingViewHeight = view.getHeight();
int movingViewWidth = view.getWidth();
int destinationTileTop = mTileHere.getTop();
int destinationTileLeft = mTileHere.getLeft();
// Transparent 'Tile Here' image (1)
// Transitional 'Tile Here' image (0 - 1)
// Opaque 'Tile Here' image (0)
float alphaMultiplier = 0;
switch(view.getId()) {
case R.id.item_position_2:
// Only allow sliding down from center of object (positive eventY)
if (eventY > movingViewHeight / 2) {
// Calculate the amount that the image has moved from it's center point
int posFromImgCenter = (eventY - movingViewHeight / 2);
// Check if the image has reached the center point and stop it's motion any farther
if (posFromImgCenter >= destinationTileTop) {
params.setMargins(view.getLeft(), destinationTileTop, 0, 0);
alphaMultiplier = 1;
}
// Slide the image with the positioning of the persons finger
else {
params.setMargins(view.getLeft(), posFromImgCenter, 0, 0);
alphaMultiplier = ((float)posFromImgCenter / (float)destinationTileTop);
}
}
// Attempting to slide in an invalid direction leave the image where it is
else
params.setMargins(view.getLeft(), view.getTop(), 0, 0);
... MORE CODE HERE FOR OTHER ITEM POSITIONS
// AFTER SWITCH STATEMENT COMPLETE UPDATE VIEW ITEMS ACCORDINGLY
// default 0 if not set with valid movement
mTileHere.setAlpha((int)(255 - (255 * alphaMultiplier)));
mTempImage.setLayoutParams(params);
mTempImage.invalidate();
Thanks,
DMan
I want a div element on my page which increases a number on the page while it is pressed.
This is fairly easy, but I want this to work on IPad and Android devices as well.
I am using the touchstart event using jQuery bind on the div.
Using a setInterval I update the number (I could use calling a setTimeout every time the number is increased, but that is irrelevant).
I want the interval cleared when the finger (touch) moves outside the div. Unfortunately touchend is not called until the finger is released from the screen.
Mobile safari or webkit do not seem to support touchleave.
So here my question: Any ideas on how to mimic touchleave?
Based on above answer, here's what I do.
var $this = $('elementselector');
var fnmove = function (e) {
e.preventDefault();
e = e.originalEvent;
var touch = e.touches[0] || e.changedTouches[0];
if (!_isInBounds(touch, $this.offset(), $this.outerWidth(), $this.outerHeight())) {
$this.trigger(touch_leave_event);
$this.unbind(touch_move_event, fnmove);
};
};
$this.bind(touch_move_event, fnmove);
-- inbounds function
function _isInBounds(touch, elemposition, width, height) {
var left = elemposition.left,
right = left + width,
top = elemposition.top,
bottom = top + height,
touchX = touch.pageX,
touchY = touch.pageY;
return (touchX > left && touchX < right && touchY > top && touchY < bottom);
};
EDIT (thanks to Guillaume to completing this):
var $this = $('elementselector');
var fnmove = function (e) {
e.preventDefault();
e = e.originalEvent;
var touch = e.touches[0] || e.changedTouches[0];
if (!_isInBounds(touch, $this.offset(), $this.outerWidth(), $this.outerHeight())) {
$this.trigger(touch_leave_event);
$this.unbind(touch_move_event, fnmove);
};
};
$this.bind(touch_move_event, fnmove);
function _isInBounds(touch, elemposition, width, height) {
var left = elemposition.left,
right = left + width,
top = elemposition.top,
bottom = top + height,
touchX = touch.pageX,
touchY = touch.pageY;
return (touchX > left && touchX < right && touchY > top && touchY < bottom);
};
Sorry for the short answer, I'm leaving work. I'll come back to fill in the blanks. But you should get the idea.
The best method I've found is to check the position of the touchpoint inside your touchmove handler. When a touchpoint is created (on touchstart), it is added to an array called "touches". These touchpoints can be accessed by index (order in which the touchpoints were added). Then you can access the x and y coordinates from this point to determine whether or not it's still within the bounds of the element you're observing. Let's say I have a div called "myDiv" and I want to detect when the user's finger is dragged out of it from within.
var myDiv = document.getElementById('myDiv');
myDiv.addEventListener('touchmove', function(event){
var currentElement = document.elementFromPoint(event.touches[0].clientX, event.touches[0].clientY);
if (currentElement.id !== 'myDiv'){
console.log('finger has moved outside of element');
}
}, false);
So when we move our finger, we capture over which element it currently is with the document.elementFromPoint() function, passing in the x and y coordinates gathered from the touch stored in the touches array, at index 0 (addition touches would be added to the array at index 1, 2, 3, etc.). Lastly, we check if this captured element where our finger currently is is the same as "myDiv". If it isn't, we know the finger has been moved off of this element.
I want to know how I can detect child views if I move a view from one ViewGroup to another ViewGroup, particularly when doing a touch event. Is there a method I can call that will let me know which views i'm "hovering" over?
What I'm doing right now is when I detect an ACTION_MOVE event on my view i'm raising it to the top level parent so that it can move and be drawn within the entire window ( and not just inside it's original parent bounds ), then I want to move the view across to a different ViewGroup and on ACTION_UP attach the view to that ViewGroup.
Inspired by Ami's response, but discovering that MotionEvent#getX()/getY() along with View#getTop()/etc return coordinates wrt the parent View, I ended up doing the following below to operate in screen coordinates, allowing me to work across ViewGroups:
private boolean inRegion(float x, float y, View v) {
v.getLocationOnScreen(mCoordBuffer);
return mCoordBuffer[0] + v.getWidth() > x && // right edge
mCoordBuffer[1] + v.getHeight() > y && // bottom edge
mCoordBuffer[0] < x && // left edge
mCoordBuffer[1] < y; // top edge
}
whose usage inside an OnTouchListener is e.g.:
boolean inside = inRegion(event.getRawX(), event.getRawY(), targetView);
I think I found a simpler way to do this.
Create an ArrayList of possible targets
Call this method from your touch event, supplying your targets list and the coords
private View findView(float x, float y, ArrayList<View> targets)
{
final int count = targets.size();
for (int i = 0; i < count; i++) {
final View target = targets.get(i);
if (target.getRight() > x && target.getTop() < y
&& target.getBottom() > y && target.getLeft() < x) {
return target;
}
}
return null;
}
I found Sebastian Roth's answer very helpful with resources, but since it wasn't really an answer to my question, I thought I'd share what I came up with.
Here is the code I use to detect views ( only views that will accept a drop that is ) given a coordinate on the screen.
private DropView findDropTarget( int x, int y, int[] dropCoordinates ){
final Rect r = mRectTemp;
final ArrayList<DropView> dropTargets = ((main) context).getBoardDropTargets();
final int count = dropTargets.size();
for (int i=count-1; i>=0; i--) {
final DropView target = dropTargets.get(i);
target.getHitRect(r);
target.getLocationOnScreen(dropCoordinates);
r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
if (r.contains(x, y)) {
dropCoordinates[0] = x - dropCoordinates[0];
dropCoordinates[1] = y - dropCoordinates[1];
return target;
}
}
}
Ok, first off mRectTemp is just an allocated Rectangle so you don't have to keep creating new ones ( I.E. final Rect r = new Rect() )
The next line dropTargets is a list of views that will accept a drop in my app.
Next I loop through each view.
I then use getHitRect(r) to return the screen coordiantes of the view.
I then offset the coordiantes to account for the notification bar or any other view that could displace the coordiantes.
finally I see if x and y are inside the coordinates of the given rectangle r ( x and y are the event.rawX() and event.rawY() ).
It actually turned out to be simpler then expected and works very well.
Read this:
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)
http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)
I had implemented a Drag and Drop using that method.
I also highly recommend a read of the HomeScreen sourcecode, which contains this thing (kind of):
https://android.googlesource.com/platform/packages/apps/Launcher2