CGPath not working in Swift 3 [duplicate] - swift3

This question already has an answer here:
1. CGPathMoveToPoint' is unavailable: Use move(to:transform:) 2. 'CGPathAddLineToPoint' is unavailable: Use addLine(to:transform:)
(1 answer)
Closed 6 years ago.
I am updating code for swift 3. I have created a custom path for my spritekit object that works in swift 2. However, now I get a compiler error:
Nil is not compatible with expected argument type 'Unsafe Pointer CGAffineTransform'
let offsetX = player.size.width * player.anchorPoint.x
let offsetY = player.size.height * player.anchorPoint.y
let path = CGMutablePath()
CGPathMoveToPoint(path, nil, 10 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 10 - offsetX, 0 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 0 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 28 - offsetX, 40 - offsetY)
CGPathAddLineToPoint(path, nil, 18 - offsetX, 46 - offsetY)
CGPathAddLineToPoint(path, nil, 6 - offsetX, 36 - offsetY)
CGPathAddLineToPoint(path, nil, 6 - offsetX, 18 - offsetY)
path.closeSubpath()
The error is for passing nil in the second argument when adding to the path. I attempted to pass in an unsafe pointer as so:
var tr = CGAffineTransform.identity
CGPathMoveToPoint(path, &tr, 10 - offsetX, 16 - offsetY)
....
but then got another strange error.
CGPathMoveToPoint is unavailable. Use move(to: transform:)
However, there was no move function with argument name to. There was a move(toParent: ) however.

Most of the syntaxes are changed in Swift 3, But they didn't removed any API methods(like CGPathMoveToPoint) and It's just renamed like below.
let path = CGMutablePath()
path.move(to: CGPoint(x: 10.0, y: 10.0))
path.addLine(to: CGPoint(x: 10.0, y: 10.0))

Related

SwiftUI Drag & Drop - NSInternalInconsistencyException: Could not get the cell at indexPath

I have implemented drag & drop between two lists in SwiftUI. Regular drag and drops work fine when dropping it in between list items, but when I do some more advanced drag like dragging it onto the cell itself, my app crashes.
This crash happens even before I drop it (so while I am still dragging it, without releasing drop)
Has anyone occurred this crash ? Is this a SwiftUI bug or am I doing something wrong ?
I have tried removing my code blocks inside dropDestinations and yet crash still exists, so what could I be doing wrong ?
Here is the gif of the crash (crash occurs right after I drag it onto the second cell, without dropping/releasing it):
Code inside dropDestination does not even get called.
SwiftUI Code
var body2: some View {
HStack {
VStack {
Text("title-pending")
List {
ForEach(viewModel.pendingOrders) { order in
RestaurantOrderCardView(order: order) {
.background(cardBackground)
.draggable(order)
}
.listRowSeparator(.hidden)
.listRowBackground(Color.clear)
.padding(.top)
}
.listStyle(.plain)
.background(listBackground)
}
VStack {
Text("title-in-preparation")
List {
ForEach(viewModel.inPreparationOrders) { item in
RestaurantOrderCardView(order: item)
.background(cardBackground)
}
.dropDestination(for: RestaurantOrder.self) { items, offset in
guard let firstItem = items.first else { return }
withAnimation {
viewModel.dropItem(firstItem, offset: offset)
}
}
.listRowSeparator(.hidden)
.listRowBackground(Color.clear)
.padding(.top)
}
.listStyle(.plain)
.background {
if !viewModel.inPreparationOrders.isEmpty {
listBackground
}
}
.overlay {
if viewModel.inPreparationOrders.isEmpty {
RoundedRectangle(cornerRadius: 8)
.foregroundColor(!inDropArea ? .grayBackgroundColor : .grayBackgroundColor.opacity(0.25))
.ignoresSafeArea()
.dropDestination(for: RestaurantOrder.self) { items, location in
guard let firstItem = items.first else { return false }
withAnimation {
viewModel.dropItem(firstItem, offset: 0)
}
return true
} isTargeted: { inDropArea in
withAnimation {
self.inDropArea = inDropArea
}
}
}
}
}
}
}
Crash Log
2023-01-06 11:25:05.995303+0100 SimplifyStayAdmin[78761:2158775] *** Assertion failure in -[_UICollectionViewDragAndDropController _beginDragAndDropInsertingItemAtIndexPath:], _UICollectionViewDragAndDropController.m:620
2023-01-06 11:25:06.074407+0100 SimplifyStayAdmin[78761:2158775] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not get the cell at indexPath <NSIndexPath: 0x8846e25efd6dfbf1> {length = 2, path = 0 - 0} to start the reording portion of the Drag-and-drop'
*** First throw call stack:
(
0 CoreFoundation 0x000000018040e7c8 __exceptionPreprocess + 172
1 libobjc.A.dylib 0x0000000180051144 objc_exception_throw + 56
2 Foundation 0x0000000180b13b98 _userInfoForFileAndLine + 0
3 UIKitCore 0x0000000109a546ec -[_UICollectionViewDragAndDropController _beginDragAndDropInsertingItemAtIndexPath:] + 540
4 UIKitCore 0x0000000109a53064 -[_UICollectionViewDragAndDropController beginReorderingForItemAtIndexPath:cell:] + 204
5 UIKitCore 0x0000000109a1bad0 -[UICollectionView _beginInteractiveMovementForItemAtIndexPath:] + 196
6 UIKit 0x000000011803040c -[UICollectionViewAccessibility beginInteractiveMovementForItemAtIndexPath:] + 80
7 UIKitCore 0x0000000109a5c55c -[_UICollectionViewDragDestinationController _reorderingDisplayLinkDidTick] + 912
8 QuartzCore 0x0000000187dd04f8 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 808
9 QuartzCore 0x0000000187ec89a8 _ZL22display_timer_callbackP12__CFMachPortPvlS1_ + 336
10 CoreFoundation 0x000000018033ee94 __CFMachPortPerform + 172
11 CoreFoundation 0x000000018037387c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
12 CoreFoundation 0x0000000180372e9c __CFRunLoopDoSource1 + 496
13 CoreFoundation 0x000000018036d43c __CFRunLoopRun + 2176
14 CoreFoundation 0x000000018036c7a4 CFRunLoopRunSpecific + 584
15 GraphicsServices 0x0000000188ff7c98 GSEventRunModal + 160
16 UIKitCore 0x000000010a1f237c -[UIApplication _run] + 868
17 UIKitCore 0x000000010a1f6374 UIApplicationMain + 124
18 SwiftUI 0x000000010e6150d4 OUTLINED_FUNCTION_51 + 496
19 SwiftUI 0x000000010e614f7c OUTLINED_FUNCTION_51 + 152
20 SwiftUI 0x000000010dd7ab60 OUTLINED_FUNCTION_10 + 88
21 SimplifyStayAdmin 0x000000010044d0c4 $s17SimplifyStayAdmin0abC3AppV5$mainyyFZ + 40
22 SimplifyStayAdmin 0x000000010044d378 main + 12
23 dyld 0x00000001080c1fa0 start_sim + 20
24 ??? 0x00000001081c9e50 0x0 + 4431060560
25 ??? 0x0869800000000000 0x0 + 606156362346397696
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not get the cell at indexPath <NSIndexPath: 0x8846e25efd6dfbf1> {length = 2, path = 0 - 0} to start the reording portion of the Drag-and-drop'
terminating with uncaught exception of type NSException
CoreSimulator 857.14 - Device: iPad (10th generation) (D79A0AD8-4970-4F84-A5F9-078DAFB697A3) - Runtime: iOS 16.2 (20C52) - DeviceType: iPad (10th generation)

Animations bound to array values causing EXC_BAD_ACCESS

I have a swiftUI view that has a number of animations which are bound to an array of structs. When I test the app on my phone I keep getting EXC_BAD_ACCESS. This is what the top of the backtrace looks like:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16f7cfff0)
frame #0: 0x00000001892665c8 SwiftUI`SwiftUI.DefaultCombiningAnimator.value(in: τ_0_0.Value, for: Swift.Double) -> Swift.Optional<τ_0_0.Value> + 48
frame #1: 0x00000001892fac40 SwiftUI`SwiftUI.AnimatorBox.value(in: τ_0_0.Value, for: Swift.Double) -> Swift.Optional<τ_0_0.Value> + 168
frame #2: 0x0000000189264a28 SwiftUI`SwiftUI.AnyAnimator.value(in: τ_0_0, for: Swift.Double) -> Swift.Optional<τ_0_0> + 156
frame #3: 0x0000000189266898 SwiftUI`SwiftUI.DefaultCombiningAnimator.value(in: τ_0_0.Value, for: Swift.Double) -> Swift.Optional<τ_0_0.Value> + 768
frame #4: 0x00000001892fac40 SwiftUI`SwiftUI.AnimatorBox.value(in: τ_0_0.Value, for: Swift.Double) -> Swift.Optional<τ_0_0.Value> + 168
frame #5: 0x0000000189264a28 SwiftUI`SwiftUI.AnyAnimator.value(in: τ_0_0, for: Swift.Double) -> Swift.Optional<τ_0_0> + 156
frame #6: 0x0000000189266898 SwiftUI`SwiftUI.DefaultCombiningAnimator.value(in: τ_0_0.Value, for: Swift.Double) -> Swift.Optional<τ_0_0.Value> + 768
frame #7: 0x00000001892fac40 SwiftUI`SwiftUI.AnimatorBox.value(in: τ_0_0.Value, for: Swift.Double) -> Swift.Optional<τ_0_0.Value> + 168
frame #8: 0x0000000189264a28 SwiftUI`SwiftUI.AnyAnimator.value(in: τ_0_0, for: Swift.Double) -> Swift.Optional<τ_0_0> + 156
The previous 4000 frames all repeat the same sequence of AnyAnimator / AnimatorBox / DefaultCombiningAnimator. The only difference I can see is that in frame #0 the DefaultCombiningAnimator ends in '-> Swift.Optional<τ_0_0.Value> + 48' instead of '+ 768'.
Can anyone give me a clue as to what (if anything) this is telling me. The underlying array is constantly updated but remains a fixed size with no appends or deletions.
This is the animated view element:
ZStack {
//MARK: - Fret Cents Label
NoteLabel()
.fill(activeFret == thisFret ? .white : intonationModel.gString[thisFret].hasBeenSet ? .black : .gray)
.opacity(0.8)
.shadow(color: .black, radius: 4, x: 0, y: 3)
VStack(spacing: 0) {
Text(String(format: "%.1f", cents))
.font(Font.custom("Karantina-Regular", size: scaleHeight * 0.06))
.foregroundColor(activeFret == thisFret ? .black : .white)
}
}
.frame(width: width * 0.9 , height: scaleHeight * 0.1)
.position(x: width * 0.6, y: scaleHeight * 0.5)
.offset(y: yScaleCents * abs(cents) <= 10 ?
scaleHeight * (yScaleCents * 0.05 * -cents) :
scaleHeight * 0.5 * -(abs(cents) / cents))
.animation(.easeInOut(duration: 0.2), value: cents)
This is where it is called in the parent view:
ForEach($intonationModel.gString.indices, id: \.self) { index in
let columnWidth = fretWidth + (120 / (CGFloat(index + 1) * 4))
if index > 0 {
VStack(spacing: 0) {
IntonatorChartColumn(
thisFret: index,
scaleHeight: scaleHeight,
scaleMargin: scaleMargin,
fretboardHeight: fretboardHeight,
fretLabelHeight: fretLabelHeight,
yScaleCents: self.yScaleCents,
stringGap: stringGap,
showFretboard: $showFretboard,
cents: $intonationModel.gString[index].cents,
note: $intonationModel.gString[index].closestNote,
octave: $intonationModel.gString[index].octave,
activeFret: $intonationModel.currentFret
)
.frame(width: columnWidth)
.id(index)
}
.padding(.top, topMargin)
}
}
The app is refusing to crash when I'm looking for zombies, even though it will reliably crash when not being monitored. I'll have a go at re-working my code to avoid the use of indices (as advised by jnpdx).
I believe there were two things going on here.
It seems like the view with the animations was updating even when it was in the background. Ensuring the published variables only updated when the view was in the foreground stopped the crashing.
The incorrect use of ForEach which I've changed to the following:
ForEach(Array(zip(intonationEngine.gString.indices, intonationEngine.gString)) , id: \.0) {index, note in
Since doing this the crashes have stopped.

Assimp, OpenGL: rotating bone around its origin

I have collada (dae) model created in blender. I load the dae file using assimp and display it using opengl in c++. The model displays correctly (image below).
The sceleton has following structre:
root, id: 0
Armature, id: 2
Armature_SPINE_NAVAL, id: 3
Armature_SPINE_CHEST, id: 4
Armature_NECK, id: 5
Armature_HEAD, id: 6
Armature_NOSE, id: 7
Armature_CLAVICLE_RIGHT, id: 8
Armature_SHOULDER_RIGHT, id: 9
Armature_ELBOW_RIGHT, id: 10
Armature_WRIST_RIGHT, id: 11
Armature_HAND_RIGHT, id: 12
Armature_HANDTIP_RIGHT, id: 13
Armature_THUMB_RIGHT, id: 14
Armature_CLAVICLE_LEFT, id: 15
Armature_SHOULDER_LEFT, id: 16
Armature_ELBOW_LEFT, id: 17
Armature_WRIST_LEFT, id: 18
Armature_HAND_LEFT, id: 19
Armature_HANDTIP_LEFT, id: 20
Armature_THUMB_LEFT, id: 21
Armature_HIP_RIGHT, id: 22
Armature_KNEE_RIGHT, id: 23
Armature_ANKLE_RIGHT, id: 24
Armature_FOOT_RIGHT, id: 25
Armature_HIP_LEFT, id: 26
Armature_KNEE_LEFT, id: 27
Armature_ANKLE_LEFT, id: 28
Armature_FOOT_LEFT, id: 29
I want to apply rotation to a single bone. However after applying rotation of 90 degrees around z axis to the Armature_WRIST_RIGHT, the bone arm gets streached. Below is my code. What I'm doing wrong?
void CalculateBoneTransform(Bone* bone, glm::mat4 parentTransform)
{
std::string boneName = bone->GetBoneName();
glm::mat4 boneTransform = bone->GetLocalTransform(); // aiNode->mTransformation
if(boneName == "Armature_WRIST_RIGHT"){
float RotationAngle = 1.5708;
float x = 0 * sin(RotationAngle / 2); // x * sin()
float y = 0 * sin(RotationAngle / 2); // y * sin()
float z = 1 * sin(RotationAngle / 2); // z * sin()
float w = cos(RotationAngle / 2);
glm::quat rot(w,x,y,z);
boneTransform *= glm::toMat4(rot);
}
glm::mat4 globalTransformation = parentTransform * boneTransform;
if (m_BoneInfoMap.find(boneName) != m_BoneInfoMap.end())
{
int index = m_BoneInfoMap[boneName].id;
glm::mat4 offset = m_BoneInfoMap[boneName].offset; // mesh->mBones[boneIndex]->mOffsetMatrix
m_FinalBoneMatrices[index] = globalTransformation * offset * glm::scale(glm::vec3(0.2f,0.2f,0.2f));
}
for (Bone* j : bone->getChildren())
CalculateBoneTransform(j, globalTransformation);
}
Above is my code. What I'm doing wrong? How should I apply the rotation transform to correctly rotate chosen bones?

How to set the shared URLCache in swift 3?

This is the code we had in Swift 2. What is the Swift 3 version? I don't see a replacement for setShared.
let sharedCache: NSURLCache = NSURLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
NSURLCache.setSharedURLCache(sharedCache)
This works in Xcode 8 Beta 4
URLCache.shared = sharedCache
Here is an Example in Swift 3 increasing cache size to 500 MB
let memoryCapacity = 500 * 1024 * 1024
let diskCapacity = 500 * 1024 * 1024
let cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: "myDataPath")
URLCache.shared = cache
It works for Xcode 8
URLCache.shared = {
URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
}()

Animating an arc in Raphael JS wobbles in Chrome

I am seeing an annoying wobble in my animation, I have stripped out the code which contains the actual animation:
var side = 400;
var paper = new Raphael($(this), 100, side);
paper.customAttributes.arc = function (xloc, yloc, value, total, R) {
var alpha = 360 / total * value,
a = (90 - alpha) * Math.PI / 180,
x = xloc + R * Math.cos(a),
y = yloc - R * Math.sin(a),
path;
if (total == value) {
path = [
["M", xloc, yloc - R],
["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R]
];
} else {
path = [
["M", xloc, yloc - R],
["A", R, R, 0, +(alpha > 180), 1, x, y]
];
}
return {
path: path
};
};
var arcWidth = 180 - 120;
var strokeRadius = (120 + arcWidth/2);
var indicatorArc = paper.path().attr({
"stroke": "#4B6384",
"stroke-width": 100,
arc: [side/2, side/2, 0, 100, strokeRadius]
});
indicatorArc.animate({
arc: [side/2, side/2, 75, 100, strokeRadius]
}, 1500, "<>", function(){
// anim complete here
});
I have put it in a jsfiddle for you to see for yourself, please check in firefox and chrome, you will notice in chrome the edges wobble substantially, is there anything I can do to eliminate this?
fiddle here: run many times
So I know this is over two years old now, but if anyone comes across this issue, it's a known bug within Chrome's 2D drawing engine.
https://code.google.com/p/skia/issues/detail?id=2769