我在public void onBindViewHolder(EntryViewHolder holder, int position) {
final EntryViewHolder h = holder;
final Cursor cursor = mContext.getContentResolver().query(Uri.parse(
queryUri), null, null, null, sortOrder);
if (cursor != null) {
if (cursor.moveToPosition(position)) {
int indexWord = cursor.getColumnIndex(Contract.WordList.KEY_ENTRY);
word = cursor.getString(indexWord);
holder.entryItemView.setText(word);
int indexNickDate = cursor.getColumnIndex(Contract.WordList.KEY_NICKNAME_DATE);
nickdate=cursor.getString(indexNickDate);
holder.tvdatetime.setText(nickdate);
int indexId = cursor.getColumnIndex(Contract.WordList.KEY_ID);
id = cursor.getInt(indexId);
int indexHeart= cursor.getColumnIndex(Contract.WordList.KEY_HEART);
heart= cursor.getInt(indexHeart);
if(heart==0){ holder.imageHeart.setImageResource(R.drawable.like);}
else if(heart==1){ h.imageHeart.setImageResource(R.drawable.heart);}
// Toast.makeText(mContext, "constant.nickname : " +nickdate , Toast.LENGTH_SHORT).show();
} else {
holder.entryItemView.setText(R.string.error_no_word);
}
cursor.close();
} else {
Log.e (TAG, "onBindViewHolder: Cursor is null.");
}
holder.imageHeart.setOnClickListener(new MyButtonOnClickListener(id, word, heart) {
@Override
public void onClick(View v) {
if (heart==0){
h.imageHeart.setImageResource(R.drawable.heart);
heart=1;
WordListOpenHelper2 woh2= new WordListOpenHelper2(mContext);
woh2.insert(word, nickdate, heart);
Toast.makeText(v.getContext(), word + " " + nickdate , Toast.LENGTH_LONG).show();
///HERE word is always true but nickdates are mixed. Couldnt figure out why:/
}
中有1个UIButton
,就像下面的屏幕所示,并且通过遵循此Answer,我将StoryBoard
从一个位置移动到另一个位置。
修改
我想旋转UIButton
的另一件事,为此我尝试了以下代码,并且工作正常,但是在旋转UIButton
时,当我尝试将UIButton
的位置从1个位置移动到另一个UIButton
帧被更改。
整个UIButton
代码。
UIButton
编辑1
编辑2
如果我分别使用class DraggableButton: UIButton {
var localTouchPosition : CGPoint?
var lastRotation: CGFloat = 0
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// ROTATION CODE
let rotate = UIRotationGestureRecognizer(target: self, action:#selector(rotatedView(_:)))
self.addGestureRecognizer(rotate)
}
// SCROLLING CONTENT FROM ONE POSITION TO ANOTHER
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let touch = touches.first
self.localTouchPosition = touch?.preciseLocation(in: self)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
let touch = touches.first
guard let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{
return
}
self.frame.origin = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y)
print(self.frame)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
self.localTouchPosition = nil
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
self.localTouchPosition = nil
}
// ROTATION CODE
@objc func rotatedView(_ sender: UIRotationGestureRecognizer) {
var originalRotation = CGFloat()
if sender.state == .began {
sender.rotation = lastRotation
originalRotation = sender.rotation
} else if sender.state == .changed {
let newRotation = sender.rotation + originalRotation
sender.view?.transform = CGAffineTransform(rotationAngle: newRotation)
} else if sender.state == .ended {
lastRotation = sender.rotation
}
}
}
旋转和运动代码,那么它可以工作,但是当我同时编写这两个代码时,会产生此问题。
答案 0 :(得分:3)
我认为问题在于旋转是围绕视图的中心,因此,当您应用旋转时,框架尺寸会增加,这会偏离相对于框架原点的距离。
我能够通过相对于视图中心移动视图来解决此问题:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let touch = touches.first
guard let location = touch?.location(in: self.superview) else { return }
// Store localTouchPosition relative to center
self.localTouchPosition = CGPoint(x: location.x - self.center.x, y: location.y - self.center.y)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
let touch = touches.first
guard let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{
return
}
self.center = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y)
print(self.frame)
}