Changing UIView's center too slow - ios

I have created 6 UIViews and added them to my main view. Then I added a UIPanGestureRecognizer to my main view. The target for the UIPanGestureRecognizer looks like this:
func panned(sender: UIPanGestureRecognizer) {
if(sender.state == UIGestureRecognizerState.Changed) {
var ty = sender.translationInView(self.view).y
for v in self.view.subviews as [UIView {
v.center.y += ty;
}
sender.setTranslation(CGPointZero, inView: self.view);
}
}
Whenever I pan, the UIViews move, but it laggs pretty much.
How can I fix this?
Thanks in advance.

Related

dragging an UIImageView on an UIView always stacks the image under the UIView

I am rather new to iOS and swift 3 programming. At the moment I am working on a little learning project. I have the following problem.
On my screen I have 4 UIViews and 4 UIImageViews. The app allows to “drag and drop” an image onto one of the UIViews. This works pretty well so far. I use Pan Gesture Recognisers for the dragging of UIImages. But for some reason one of the UIViews seems to be in front of the image, meaning that I can drop the image to this area, but I cannot see it - it is layered under the UIView. The other three behave properly, the UIImageView is layered on top of the UIView. In the viewDidLoad() function of the view controller I set the background color of the UIViews to grey. If I don’t do that, the image will be displayed even on the fourth UIView.
I have checked for differences between the four UIView objects but cannot find any. I also tried to recreate the UIView by copying it from one of the the other three.
So my question is: Is there any property which defines that the UIView is layered on top or can be sent to back so that other objects are layered on top of it? Or is there anything else I am missing? Any hints would be very appreciated.
I was not quite sure which parts of the code are relevant, so I posted some of it (but still guess that this is rather a property of the UIView…)
This is where I set the background of the UIViews
override func viewDidLoad() {
super.viewDidLoad()
lbViewTitle.text = "\(viewTitle) \(level) - \(levelPage)"
if (level==0) {
print("invalid level => EXIT")
} else {
loadData(level: level)
originalPositionLetter1 = letter1.center
originalPositionLetter2 = letter2.center
originalPositionLetter3 = letter3.center
originalPositionLetter4 = letter4.center
dropArea1.layer.backgroundColor = UIColor.lightGray.cgColor
dropArea2.layer.backgroundColor = UIColor.lightGray.cgColor
dropArea3.layer.backgroundColor = UIColor.lightGray.cgColor
dropArea4.layer.backgroundColor = UIColor.lightGray.cgColor
}
}
This is the code that moves the image:
#IBAction func hadlePan1(_ sender: UIPanGestureRecognizer) {
moveLetter(sender: sender, dropArea: dropArea4, movedImage: letter1, originalPosition: originalPositionLetter1)
}
func moveLetter(sender: UIPanGestureRecognizer, dropArea : UIView, movedImage : UIImageView, originalPosition : CGPoint) {
let translation = sender.translation(in: self.view)
if let view = sender.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
if sender.state == UIGestureRecognizerState.ended {
let here = sender.location(in: self.view)
if (dropArea.frame.contains(here)) {
movedImage.center = dropArea.center
} else {
movedImage.center = originalPosition
}
}
}
sender.setTranslation(CGPoint.zero, in: self.view)
}
There are two methods that may be useful to you: UIView.sendSubview(toBack:) and UIView.bringSubview(toFront:).
What I think you should do is to call bringSubview(toFront:) when you start dragging the image:
func moveLetter(sender: UIPanGestureRecognizer, dropArea : UIView, movedImage : UIImageView, originalPosition : CGPoint) {
self.view.bringSubview(toFront: movedImage) // <--- add this line!
let translation = sender.translation(in: self.view)
if let view = sender.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
if sender.state == UIGestureRecognizerState.ended {
let here = sender.location(in: self.view)
if (dropArea.frame.contains(here)) {
movedImage.center = dropArea.center
} else {
movedImage.center = originalPosition
}
}
}
sender.setTranslation(CGPoint.zero, in: self.view)
}

Select part of GLKViewController

I'm try to implement this in my project. But I have some troubles with it. I plan to use UiPanGestureRecognizer for change size of rectangle. As I understand, I should be use UIVIew and custom drawRect method?
Add an action for your UIPanGestureRecognizer and use translation:
func wasDragged(gesture: UIPanGestureRecognizer) {
let translation = gesture.translationInView(self.view)
// Do your resizing here, e.g. from a
customView.frame.size.width = currentFrame.width + translation.x
customView.frame.size.height = currentFrame.height + translation.y
if gesture.state == .Ended {
currentFrame = customView.frame
}
}
Using this method, add a CGRect variable to store the currentFrame.

bringSubviewToFront(_:) function breaks a UIPanGestureRecognizer

I have a BoardView class which subclasses UIView. It has several subviews, which all have their own UIPanGestureRecognizer. I use this recognizer to drag around a subview inside my BoardView instance, as such:
func pieceDragged(recognizer: UIPanGestureRecognizer) {
let pieceView = recognizer.view!
if recognizer.state == .Began || recognizer.state == .Changed {
// move the pieceView by the appropriate amount
let translation = recognizer.translationInView(self)
pieceView.center = CGPoint(x: pieceView.center.x + translation.x, y: pieceView.center.y + translation.y)
recognizer.setTranslation(CGPoint.zero, inView: self)
} else if recognizer.state == .Ended {
// do something else
}
}
This works fine.
The problem is that I want the subview that is being dragged around to be on top of all of its siblings, so I made my BoardView instance the delegate of each gesture recognizer and overrode this function:
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
let pieceView = gestureRecognizer.view!
bringSubviewToFront(pieceView)
return true
}
This seems to break the entire gesture recognizer: if I attempt to drag aroudn a subview, the pieceDragged(_:) function is called exactly once (where the state of the recognizer is .Began), but no more than that.
If I comment out bringSubviewToFront(pieceView), my code works as before - I can drag around any subview, but it's below some of its siblings.
I'm clueless. What could be going on here?
The problem was that I replaced all my subviews in the layoutSubviews() method (which is automatically called after bringToFront(_:) is called).
Seems like you are going round the houses as well and implementing a method that doesn't really make sense. The delegate method gestureRecognizerShouldBegin(_:) is all about should this recogniser actually start detecting not a place to perform an action.
You already have a suitable place to perform the action you want you could simply update your code to be
func pieceDragged(recognizer: UIPanGestureRecognizer) {
guard let pieceView = recognizer.view else {
return
}
switch recognizer.state {
case .Began:
pieceView.superview?.bringSubviewToFront(pieceView)
case .Changed:
let translation = recognizer.translationInView(self)
recognizer.setTranslation(CGPoint.zero, inView: self)
pieceView.center = CGPoint(x: pieceView.center.x + translation.x, y: pieceView.center.y + translation.y)
default: ()
}
}

How to allow drag UIView (PanGesture..) just in some area

I need drag UIView via PanGestureRecognizer (I know how to do), but I can't figure out, how to make it with limitation. Need some padding from top and also if there is collision with one of four sides (left, right, top (here is the padding) and bottom of device) stop the drag and you can't over - or 1px padding like on the top, whatever. :)
I tried this one: https://github.com/andreamazz/UIView-draggable but if I set the area with limitation via cagingArea, iPad (Air) is lagged. Also the moving is not smooth, I think the native PanGestureRecognizer is the best, need just the limitation area, do you know how I can do that please? :)
I'm writing in Swift. And also found some related topics, like this one -> Use UIPanGestureRecognizer to drag UIView inside limited area but I don't know what insideDraggableArea doing?..
Thank you so much programmers!
Same problem that I faced in my project,
Try this,
1) Init PanGesture
let panRec = UIPanGestureRecognizer()
2) Add PanGesture to your UIView
override func viewDidLoad() {
....
....
panRec.addTarget(self, action: "draggedView:")
yourview.addGestureRecognizer(panRec)
yourview.userInteractionEnabled = true
....
....
}
3) Set your limitation on draggedView function
func draggedView(sender:UIPanGestureRecognizer){
println("panning")
var translation = sender.translationInView(self.view)
println("the translation x:\(translation.x) & y:\(translation.y)")
//sender.view.
var tmp=sender.view?.center.x //x translation
var tmp1=sender.view?.center.y //y translation
//set limitation for x and y origin
if(translation.x <= 100 && translation.y <= 50 )
{
sender.view?.center=CGPointMake(tmp!+translation.x, tmp1!+translation.y)
sender.setTranslation(CGPointZero, inView: self.view)
}
}
Do you have to use UIPanGestureRecognizer ?
add view to a viewcontroller and check user interaction enabled.
i think you should tag view and use touchesMoved method.
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
if touch.view.tag == 2 {
if self.yourView.center.y <= CGFloat(230) { //if you want use limitation drag
var touchLocation = touch.locationInView(self.view)
self.yourView.center.y = touchLocation.y
self.yourView.center.x = touchLocation.x
}
}
}
}
This works for me:
guard let view = UIApplication.shared.windows.last else { return }
view.addSubview(customView)

Check if view within other view?

I’m using UIPanGestureRecognizer to move UIImageView. Now i’m need to check if user moved my object within other vectored-PDF UIImageView.
For example we have red bar that we are moving and we need to move red bar inside blue bar (blue bar is vector PDF). How can we check that?
Example image: http://i.imgur.com/PKXKHBi.png
Example code:
#IBAction func moveRedBox(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.view)
recognizer.view!.center = CGPoint(x:recognizer.view!.center.x + translation.x,
y:recognizer.view!.center.y + translation.y)
recognizer.setTranslation(CGPointZero, inView: self.view)
if recognizer.state == UIGestureRecognizerState.Ended {
// Here we will check that redBox inside blueBox
}
}
To check if two view's overlap you can use the CGRectIntersectsRect function.
Update: Just found out that there is also an intersects method on CGRect in Swift
let doRectsIntersect = rect1.intersects(rect2)
Check this out. This helped me:
if view1.frame.contains(view2.center) {
// do your thing
}
Full example:
#IBAction func moveRedBox(recognizer: UIPanGestureRecognizer) {
...
switch recognizer.state {
case .Ended:
if blueBox.frame.contains(recognizer.view!.center) {
print("Yep")
}
}
}
Check position of UIImageView
image.frame.origin.x >= view.frame.origin.x
image.frame.origin.y >= view.frame.origin.y
put this code when the movement done

Resources