XCode 8: Puzzling side effect of setting label.text - uilabel

It appears that setting the text of a label can have the side effect of increasing the opacity of a MKCircle that has been added to an MKMapView.
Here is what's going on:
First I have this delegate function to handle rendering my overylay
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let diskRenderer: MKCircleRenderer = MKCircleRenderer.init(overlay: overlay)
diskRenderer.fillColor = UIColor.init(red: 0, green: 255, blue: 255, alpha: 0.10)
diskRenderer.strokeColor = UIColor.init(red:0, green: 0, blue: 0, alpha: 0.25)
return diskRenderer
}
Then I add it to the MKMapView
map.delegate = self
let diskOverlay: MKCircle = MKCircle.init(center: location, radius: diskRadius)
map.add(diskOverlay)
All seems good. Now at the same time, I have a custom UIControl called YSRangeSlider that I got from cocoapods. When the user moves the thumbs on this slider, a function gets called that updates the text of some labels according to whatever the current values are on the slider. Here is that function.
func setPlayerLabels() {
let lowerValue = Int(playersSlider.minimumSelectedValue)
let upperValue = Int(playersSlider.maximumSelectedValue)
minPlayersLabel.text = "\(lowerValue)"
maxPlayersLabel.text = "\(upperValue)"
}
Now here is the really really weird thing. When I launch the app and segue to this ViewController and then start sliding a thumb on the slider, the MKCircle - that is the faint disk being shown on the map, becomes more and more opaque until it is solid filled. But if I comment out the two label.text = "---" going on in the function above, this strange behavior no longer occurs.
Below you can see the overlay as it looks initially (low opacity) and then after getting messed up (opaque). I don't know the deeper reason of why this is happening nor how to fix it. This is XCode 8 with Swift 3.
To make things even worse, I should mention that sometimes the map renders the disk overlay as if it was on a large square tile casting a shadow beneath it, a shadow who's opacity goes towards opaque just like the overlay itself. Also if I zoom out some before touching the slider, it so happens that sliding a thumb on the slider then causes the map to zoom back in on top of the already mentioned opacity changes.

For anyone interested, I figured out how to solve this problem, however the fundamental cause and effect behind it is still a bit lost on me.
What's happening is whenever those label.text = "---" assignments get called, the viewDidLayoutSubviews() function also gets called. Since the MKMapView's overlay and region get set inside viewDidLayoutSubviews, it so happens that the overlay gets redrawn on top of the existing one, making it appear to get darker and darker, and hence the map zooms back into the region if I had already zoomed out.
What I still don't understand is why executing label.text = "---" causes the viewDidLayoutSubviews function to get called.

Related

How to make native (realtime) blur in Sprite-Kit?

I am using this code for my blur, but it doesn't seem to work. Or it kinda works. I can only see a grey coloured background instead of the blurred scene. (First picture, I have captured it using a screenshot, but that captures it like its working great, weird.) And when i swipe down, it blurs the scene normally.
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[blurEffectView setFrame:self.view.bounds];
[self.view addSubview:blurEffectView];
The screenshot captures it normally but i can only see grey background.
Here when i swipe down, it works like charm.
I have found different solutions for this, but those were only static. I want this type of blur, it doesn't even drop any fps. Is there a way to get this working?
Since
The blur effect is provided by a Core Image Filter.
Every Core Image Filter can be used with a SKEffectNode
SKScene is an SKEffectNode
then...
class GameScene: SKScene {
override func didMove(to view: SKView) {
super.didMove(to:view)
self.filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius": 10])
self.shouldEnableEffects = true
}
}

UIView animations - When does it animate certain properties like scale and position?

I'm currently working with a UITableViewController which contains some UITableViewCells subclasses.
When layoutSubviews is called on these UITableViewCells, I change some of the cells' subviews' scales and positions depending on the width and height of the contentView. (The UITableViewCells have some subviews, and I change their scales and positions)
A good example is toggling edit mode, since it shortens the contentView by a bit.
When I do this, the scales and positions of my UITableViewCell's subview do animate
func toggleEdit() {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.35)
self.tableView.beginUpdates()
self.tableView.editing = !self.tableView.editing
self.tableView.endUpdates()
UIView.commitAnimations()
}
When I do this, ONLY the positions animate, the scales change immediately which looks really ugly:
func toggleEdit() {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.35)
self.tableView.editing = !self.tableView.editing
UIView.commitAnimations()
}
I kind of found this out by accident, so I'm wondering now what kind of magic begin/end tableViewUpdate does, and how I can control myself in any scenario which properties should animate and which shouldn't.
The docs state
When you call endUpdates, UITableView animates the operations simultaneously
You could try changing things that you don't want animated in a performWithoutAnimation(_:) block
UIView.performWithoutAnimation {
// things you don't want to animate
}

UIScrollView - Custom Map - Prevent marker subview on map from scaling with map

I have a custom map of a limited area, and have it set up to correctly show the users' location. The map is a 1600px square image within a UIScrollView.
I have a crosshair image to show the current location of the user, which at zoomScale 1.0 is the desired size. When I pinch and zoom the scrollView, the crosshair scales with it. I would like to have the subview remain the same size on screen.
I haven't been able to find any information on this, what would be the best way to go about this?
If there is anything I can provide you with to help the answer, please let me know.
Many thanks!
EDIT -
Having looked in to this further, there is a UIScrollViewDelegate method - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale which I tried using to take the marker's current center and size, then adjust, but this only scales at the end of the zoom. I would prefer to have the marker remain the same size while the user is zooming.
EDIT 2-
Cake has provided a great answer below, but I haven't been able to implement this in the way I imagined it would be.
I have the UIImageView as a placeholder, with alpha set to 0. This placeholder moves around relative to the map to show the user location. This operates as I expect it to. Unfortunately, this resizes with the map, as it is a subview of the map (so it stays in place).
Taking Cake's below answer, I have created the non-scaling crosshair image, and added it as a sibling subview to the scrollview. The maths, once Cake had pointed them out, were quite simple to get the new frame for the crosshair:
CGPoint ULPC = userLocationPlaceholder.center;
float zs = scrollView.zoomScale;
CGRect newFrame = CGRectMake(((ULPC.x * zs) - scrollView.contentOffset.x) - 20, ((ULPC.y * zs) - scrollView.contentOffset.y) - 20, 40, 40);
Where the image is 40points wide. This matches the centers perfectly.
The problem I now have is that I cannot get the crosshair image to stay locked to the placeholder.
I have tried using a self calling animation as such:
-(void)animeUserLocationAttachment
{
[UIView animateWithDuration:0.05
delay:0
options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveLinear )
animations:^{
userLocationDotContainer.frame = newFrame;
} completion:^(BOOL finished){
// Call self
[self animateUserLocationAttachment];
}];
}
As soon as I start scrolling/zooming, this locks the animation so that the crosshair just sits in place until I release the scrolling/zooming, then it correctly updates it's location.
Is there any way I can get around this, or an alternative method I can apply?
Many thanks
EDIT 3 -
I've re-accepted Cake's answer as it covers 90% of the issue. Further to his answer I have implemented the ScrollViewDelegate methods scrollViewWillBeginDragging: andscrollViewWillBeginDecelerating: to scale the placeholder to match the current size of the crosshair relative to the map, show the placeholder (that is a subview of the map image) and hide the crosshair image. The delegate method scrollviewWillBeginZooming:withView: does not show the placeholder because it scales with the map. As Cake recommends, I'll make a new question for this issue.
The counterpart methods (scrollViewDidEndZooming:withView:atScale:, scrollViewDidEndDragging:willDecelerate: and -scrollViewDidEndDecelerating:`) all hide the placeholder, and re-show the crosshair.
The question is old but for the future similar questions I've recently resolved a similar problem applying the hint of Andrew Madsen of another post.
I'had a UIScrollView, with an UIImageView in it. Attached to the UIImageView I had many MKAnnotationView (those are my subviews that I didn't want scaling with the superview).
I did subclass UIImageView and implement setTransform: method like here:
#import "SLImageView.h"
#implementation SLImageView
- (void)setTransform:(CGAffineTransform)transform
{
[super setTransform:transform];
CGAffineTransform invertedTransform = CGAffineTransformInvert(transform);
for (id obj in self.subviews)
{
if ([obj isKindOfClass:[MKAnnotationView class]])
{
[((UIView *)obj) setTransform:invertedTransform];
}
}
}
#end
This works perfectly!
Mick.
Create another crosshair image that's associated with the view or view controller that contains the scrollview. Then have this one always snap to the center of the crosshair image you already have. Then, hide your original crosshair image. Then you can avoid having the scrollview scale the disassociated crosshair, and it should stay the same size.
Relative coordinate systems
Each view in cocoa touch has a frame property that has an origin. In order to position an object owned by one view properly relative to another view, all you have to do is figure out the differences in their origins. If one view is a subview of another, then this isn't too difficult.
Get the origin of the container view
Get the location of the subview inside of the container view
Get the origin of the subview
Calculate the difference in the positions of the origins
Get the location of the object you want to overlap (relative to the subview)
Calculate the location of the object you want to overlap relative to the container view
Move your crosshair to this position
Swift equivalent for Mick's answer:
class MapContainerView:UIView {
#IBOutlet var nonScalingViews: [UIView]!
override var transform: CGAffineTransform {
didSet {
guard let nonScalingViews = nonScalingViews else {
return
}
let invertedTransform = CGAffineTransformInvert(transform)
for view in nonScalingViews {
view.transform = invertedTransform
}
}
}
}

Infinite horizontal scrolling UIScrollView

I have a UIScrollView whose content size is 1200x480. I have some image views on it, whose width adds up to 600. When scrolling towards the right, I simply increase the content size and set the offset so as to make everything smooth (I then want to add other images, but that's not important right now). So basically, the images currently in the viewport remain somewhere on the left, eventually to be removed from the superview.
Now, the problem that I have happens when scrolling towards the left. What I do is I move the images to the end of the content size (so add 600 to each image view's origin.x), and then set the content offset accordingly. It works when the finger is on the screen and the user drags (scrollView.isTracking = YES). When the user scrolls towards the left and lets go (scrollView.isTracking = NO), the image views end up moving too fast towards the right and disappear almost instantly. Does anyone know how I could have the images move nicely and not disappear even when the user's not manually dragging the view and has already let go?
Here's my code for dragging horizontally:
-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = self.scrollView.contentOffset;
CGSize size = self.scrollView.contentSize;
CGPoint newXY = CGPointMake(size.width-600, size.height-480);
// this bit here allows scrolling towards the right
if (offset.x > size.width - 320) {
[self.scrollView setContentSize:CGSizeMake(size.width+320, size.height)];
[self.scrollView setContentOffset: offset];
}
// and this is where my problem is:
if (offset.x < 0) {
for (UIImageView *imageView in self.scrollView.subviews) {
CGRect frame = imageView.frame;
[imageView setFrame:CGRectMake
(frame.origin.x+newXY.x, frame.origin.y, 200, frame.size.height)];
}
[self.scrollView setContentOffset:CGPointMake(newXY.x+offset.x, offset.y)];
}
}
EDIT: This is now working - I had a look at StreetScroller and it's all good now.
However, I now want to zoom in on the scrollview, but viewForZoomingInScrollView is never called. Is it not possible to zoom in on a scrollview with a large content size?
There are some approaches floating around here. Just use the site search …
If you want an more "official" example created by Apple take a look at the StreetScroller Demo. For some more information about this example take a look at last years WWDC session no. 104 Advanced Scroll View Techniques.
There is also an UIScrollView subclass on Github called BAGPagingScrollView, which is paging & infinite, but it has a few bugs you have to fix on your own, because it's not under active development (especially the goToPage: method leads to problems).

How to constrain UIScrollView to only zoom vertically?

I have a UIScrollView which I'm using to represent an axis on a graph. I'd like the user to be able to zoom in on the axis using the usual pinch motion, but for it to only scale in the vertical direction, not horizontally.
My question is similar to this one, but I've tried the solution suggested there (overriding the subview's SetTransform method so that it ignores scaling in one direction) and it works perfectly when constraining scaling horizontally, but not vertically. When I try implementing it vertically the first pinch action works fine, but subsequent pinches seem to reset the zoom scale to one before having any effect.
Does anyone know what might be causing this behaviour, and more importantly how I can get around it?
I'm using MonoTouch but answers using Objective-C are fine.
I know this question was posted quite a while ago, but here's an answer for anyone stuck on this problem.
I looked over the question you linked to, rankAmateur, and I think the simple way to fix the solution found there to suit your needs is to replace the CGAffineTransform's "a" property with its "d" property in the setTransform: method.
- (void)setTransform:(CGAffineTransform)newValue;
{
CGAffineTransform constrainedTransform = CGAffineTransformIdentity;
// constrainedTransform.a = newValue.a;
constrainedTransform.d = newValue.d;
[super setTransform:constrainedTransform];
}
I'm not very well versed in CGAffineTransorm, but this worked for me and after browsing the documentation it seems the "a" property corresponds to a view's x-axis and the "d" property corresponds to a view's y-axis.
EDIT
So after going back and realizing what the question really was, I did some more digging into this and I'm a bit stumped, but having experienced the same behavior that rankAmateur mentions above, it seems incredibly unusual for the CGAffineTransform to work perfectly well with zoomScale when zooming is constrained to only horizontally, but not when constrained to only vertically.
The only hypothesis I can offer, is that it might have something to do with the differing default coordinate systems of Core Graphics and UIKit, since in those coordinate systems the x-axis functions in the same way, while the y-axis functions oppositely. Perhaps somehow this gets muddled up in the previously mentioned overriding of setTransform.
This answer depends heavily on the answer from starryVere (thumbs up!)
this is starryVere's code in Swift. It is in the zoomed UIView subclass:
var initialScale: CGFloat = 1.0
override var transform: CGAffineTransform {
set{
//print("1 transform... \(newValue), frame=\(self.frame), bounds=\(self.bounds)")
var constrainedTransform = CGAffineTransformIdentity
constrainedTransform.d = self.initialScale * newValue.d // vertical zoom
//constrainedTransform.a = newValue.a // horizontal zoom
super.transform = constrainedTransform
//print("2 transform... \(constrainedTransform), frame=\(self.frame), bounds=\(self.bounds)")
}
get{
return super.transform
}
}
The commented out prints are very helpful to understand what happens with bounds and frame during the transformation.
Now to the scale problem:
the method scrollViewDidEndZooming of the containing UIScrollViewDelegate has a parameter scale. According to my tests this parameter scale contains the value zoomedView.transform.a which is the horizontal scale factor that we set to 1.0 using CGAffineTransformIdentity. So scale is always 1.0.
The fix is easy:
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
let myScale = zoomView.transform.d
}
use myScale like you would use scale in cases with horizontal zoom.
After struggling with the same issue, I was able to come up with a workaround.
Use this code for the setTransform method.
-(void)setTransform:(CGAffineTransform)transform
{
CGAffineTransform constrainedTransform = CGAffineTransformIdentity;
constrainedTransform.d = self.initialScale * transform.d;
constrainedTransform.d = (constrainedTransform.d < MINIMUM_ZOOM_SCALE) ? MINIMUM_ZOOM_SCALE : constrainedTransform.d;
[super setTransform:constrainedTransform];
}
Set the initialScale property from within the scrollViewWillBeginZooming delegate method.
It will be more helpful if you provide a sample code of what you are trying, but i am giving you some lines to try. Actually you have to make the content size width equal to "320" i.e. equal to the the screen size of iPhone.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 45,320,480)];
scrollView.contentSize = CGSizeMake(320,1000);
scrollView.showsVerticalScrollIndicator = YES;
The MonoTouch version follows:
scrollView = new UIScrollView (new RectangleF (0, 45, 320, 480)) {
ContentSize = new SizeF (320, 1000),
ShowVerticalScrollIndicator = true
};
Hope it helps.. :)
and Yes dont forget to accept the answer if it helps :D